Upload JS Verification (image/File extension) _javascript tips

Source: Internet
Author: User
js verification upload picture
Copy Code code as follows:

var imgobj=new image ()//Create an Object
Varallimgext= ". Jpg|. Jpeg|. Gif|. bmp|. Png| " All Picture format types
varfileobj,imgfilesize,imgwidth,imgheight,fileext,errmsg,filemsg,isimg//global variable Picture related properties
The following are restricted variables
var allowext= ""; Allow uploaded file type Ŀ for unrestricted each extension with a "|" lowercase letter indicating
var allowimgfilesize=100;//allow upload picture file size 0 is unrestricted: KB
var allowimgwidth=385; The width of the picture allowed to be uploaded is Ɓ to an unrestricted unit: PX (pixel)
var allowimgheight=441; Allow upload of picture height Ƹ to unrestricted unit: PX (pixel)
function Checkproperty (obj)//Detect image Properties
{
Fileobj=obj;
if (imgobj.readystate!= "complete")//If the image is not loaded complete for loop detection
{
SetTimeout ("Checkproperty (Fileobj)", 500);
return false;
}
Imgfilesize=math.round (imgobj.filesize/1024*100)/100;//Gets the size of the picture file
imgwidth=imgobj.width;//get the width of the picture
Imgheight=imgobj.height; Get the height of the picture
Filemsg= "\ n picture size:" +imgwidth+ "*" +imgheight+ "px";
filemsg=filemsg+ "\ n picture file size:" +imgfilesize+ "Kb";
filemsg=filemsg+ "\ n picture file name extension:" +fileext+ "\ n can upload! ";
Errmsg= "";
if (allowimgwidth!=imgwidth)
errmsg=errmsg+ "\ n please upload width equal to" +allowimgwidth+ "px file, the current picture width is" +imgwidth+ "px";
if (allowimgheight!=imgheight)
errmsg=errmsg+ "\ n please upload height equals" +allowimgheight+ "px file, the current picture height is" +imgheight+ "px";
if (allowimgfilesize!=0&&allowimgfilesizeerrmsg=errmsg+ "\ n please upload the file less than" +allowimgfilesize+ "KB, the current file size is" +imgfilesize+ "KB;
if (errmsg!= "")
{
alert (errmsg);
return false;
}
Else
return true;
}//end Checkproperty ();
Imgobj.onerror=function () {errmsg= ' \ \ n picture is not well-formed or the picture is corrupted! '}
function Checkext (obj)
{
Errmsg= "";
Filemsg= "";
Isimg=false;
if (obj.value== "")
return false;
Fileext=obj.value.substr (Obj.value.lastIndexOf (".")). toLowerCase ();
if (Allimgext.indexof (fileext+ "|")! =-1)//If picture file, then do image information processing
{
Isimg=true;
Fileobj=obj;
Imgobj.src=obj.value;
Returncheckproperty (obj);
}else
{
Alert ("This file type is not allowed to upload.) Please upload "+allimgext+" type of file, \ n The current file type is "+fileext";
Obj.value= ';
return false;
}
}

js verification of upload
The following is a description of how to control the extension of an uploaded file.
Js:
Copy Code code as follows:

function Check2 ()
{
var file = document.getelementsbyname ("file"). Value;
if (file== "")
{
Alert ("Please select File");
return false;
}
var strtemp = File.split (".");
var strcheck = strtemp[strtemp.length-1];
if (strcheck.touppercase () = = ' JPG ')
{
return true;
}else
{
Alert (' Upload file type is wrong! ');
return false;
}
}

Form:
Copy Code code as follows:

<form action= "*.jsp" method= "POST" onsubmit= "return Check ()" >
<input type= "File" name= "file" >
<input type= "Submit" value= "Upload" >
</form>

It should be noted that Document.getelementsbyname ("file"). Value gets the absolute path of the uploaded file, so the string segmentation method is used to separate the extended name of the file and then judge it.

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.