JS Picture Verification Size

Source: Internet
Author: User

Verify file size only, do not validate pixels

<script>

function Check () {

var size= upload file control. Postedfile.contentlength; Get the size of the uploaded file

if (size>120) {

Alert ("Picture is greater than 120kb");

return false;

}

}

</script>

Verify pixel size

<input name= "txtimg" type= "file" id= "Txtimg"/>
<input name= "btnuploadimg" type= "button" id= "btnuploadimg" value= "Upload"/>
<span class= "Redcolor" > (Best Picture size: Width not exceeding 600 pixels, height not exceeding 600 pixels) </span>


<script language= "JavaScript" >
var allowimgfilesize=512; Allow upload of picture file size 0 for unrestricted units: KB
var allowimgwidth=600; The width of the allowed image to be uploaded is 0 unrestricted: px (pixels)
var allowimgheight=600; The height of the image allowed to upload is 0 unrestricted: px (pixels)

function checkimg () {
var Txtimg=document.getelementbyid ("Txtimg");
if (txtimg.value== "") {
Alert ("Click the browse button, select the JPG or GIF file you want to upload!");
Txtimg.focus ();
return false;
}
&NBSP;
   var txtimg_url=txtimg.value.tolowercase ();
   var txtimg_ext=txtimg_url.substring (txtimg_url.length-3,txtimg_url.length);
   if (txtimg_ext!= "JPG" && txtimg_ext!= "gif") {
    alert ("Please select files in jpg or GIF format!");
    txtimg.select ()
    document.execcommand ("Delete");
    Txtimg.focus ();
     return false;
 }
  var errmsgerrmsg = "";//error message
  var img=new Image ();
  Img.src=txtimg.value;
 &NBSP;
  if (Img.width > Allowimgwidth) {
   Errmsgerrmsg= "\ n \ nthe picture width exceeds the limit please upload the width less than" +allowimgwidth+ "px file, the current picture width is" +img.width+ "px"; &NBSP;
  alert (errmsgerrmsg);   return false;
 

if (Img.height > Allowimgwidth) {
Errmsgerrmsg= "\ n \ nthe picture height exceeds the limit please upload the file height less than" +allowimgheight+ "px, the current picture height is" +img.height+ "px";&NBSP;
   alert (errmsgerrmsg);    return false;
 
 &NBSP;
  var size = Formatnum (img.filesize/1024,2);
  if (Size > Allowimgfilesize)   {
  errmsgerrmsg = "\ n \ nthe picture file size exceeds the limit please upload less than" +allowimgfilesize+ " KB of files, the current file size is "+size+" KB "; &NBSP;
  alert (errmsgerrmsg);   return false;
 }

Alert (' ok! ');
return true;
}

</script>

JS Picture Verification Size

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.