JavaScript validation upload picture type function detailed

Source: Internet
Author: User
Tags sleep

The foreground call is as follows

The code is as follows Copy Code
onclientclick= "Return Fuccheckjpgandgif" (Form1. File1.value); "

--where File1 is the upload file control

Function code:

The code is as follows Copy Code

Function Fuccheckjpgandgif (strfilename) {
    if (strFileName!= "") {
         var strtype = strfilename.substring (strfilename.length-4, strfilename.length);
        strtype = Strtype.tolowercase ();
        if ((strtype = ". jpg") | | (Strtype = = ". gif") | | (strtype = = "jpeg") | | (Strtype = = ". bmp")) {
            return true;
       }
        else {
             alert ("Please upload a picture in jpg, GIF or BMP format, thank you!") ");
            return false;
       }
   }
    alert ("Please upload a picture in jpg, GIF, or BMP format, thank you!") ");
    return false;

}

Below put a compatibility super other upload file picture type verification

The code is as follows Copy Code

<script>

Uploadfilecheck=function ()
{
This. allowext= ". Jpg,.gif";//Allow upload of file type 0 for unrestricted each extension must be added with a "," lowercase letter
This. allowimgfilesize=0;//allowed to upload files of size 0 to unlimited units: KB
This. allowimgwidth=0;//allowed to upload a picture with a width of 0 of unlimited units: px (pixel)
This. allowimgheight=0;//allowed to upload a picture of the height of 0 is an unrestricted unit: PX (pixel)
This. Imgobj=new Image ();
This. imgfilesize=0;
This. imgwidth=0;
This. imgheight=0;
This. Fileext= "";
This. Errmsg= "";
This. isimg=false;//Global Variables

}

Uploadfilecheck.prototype.checkext=function (obj)
{
This. Errmsg= "";
This. Imgobj.src=obj.value;
This. Haschecked=false;
if (obj.value== "")
{
This. errmsg= "N Please select a file";
}
Else
{
This. Fileext=obj.value.substr (Obj.value.lastIndexOf (".")). toLowerCase ();
if (this. Allowext!=0&&this. Allowext.indexof (this. Fileext)//To determine whether the file type is allowed to be uploaded or not ==-1
{
This. errmsg= "n This file type is not allowed to upload. Please upload "+this." allowext+ "Type of file, the current file type is +this. Fileext;
}
}
if (this. Errmsg!= "")
{
This. ShowMsg (this. Errmsg,false);
return false;
}
Else
return this. Checkproperty (obj);
}

Uploadfilecheck.prototype.checkproperty=function (obj)
{
while (this. imgobj.readystate!= "complete")//
{
Sleep (1000);//One second use diagram to fully load
}

if (this. Isimg==true)
{
This. Imgwidth=this. imgobj.width;//get the width of the picture
This. Imgheight=this. imgobj.height;//get the height of the picture
if (this. Allowimgwidth!=0&&this. Allowimgwidth<this. ImgWidth)
This. Errmsg=this. errmsg+ "n picture width exceeds limit. Please upload width less than "+this." allowimgwidth+ "px file, the current picture width is" +this. imgwidth+ "px";

if (this. Allowimgheight!=0&&this. Allowimgheight<this. ImgHeight)
This. Errmsg=this. errmsg+ "n picture height exceeds limit. Please upload height less than "+this." allowimgheight+ "px file, the current picture height is" +this. imgheight+ "px";
}

This. Imgfilesize=math.round (this. IMGOBJ.FILESIZE/1024*100)/100;//Get the size of the picture file
Alert (this. Imgfilesize);
if (this. Allowimgfilesize!=0&&this. Allowimgfilesize<this. Imgfilesize)
This. Errmsg=this. errmsg+ "n file size exceeds limit. Please upload less than "+this." Allowimgfilesize+ "KB file with the current file size of +this. Imgfilesize+ "KB";

if (this. Errmsg!= "")
{
This. ShowMsg (this. Errmsg,false);
return false;
}
Else
return true;
}

Uploadfilecheck.prototype.showmsg=function (MSG,TF)//display hint information tf=false display error message msg-information content
{
/*msg=msg.replace ("n", "<li>");
Msg=msg.replace (/n/gi, "<li>");
*/
Alert (msg);
}
function sleep (num)
{
var tempdate=new Date ();
var tempstr= "";
var thexmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
while (new Date ()-tempdate) <num)
{
tempstr+= "n" + (new Date ()-tempdate);
try{
Thexmlhttp. Open ("Get", "About:blank?") Jk= "+math.random (), false);
Thexmlhttp. Send ();
}
catch (e) {;}
}
CONTAINERDIV.INNERTEXT=TEMPSTR;
Return
}

function C (obj)
{
var d=new uploadfilecheck ();
D.isimg=true;
d.allowimgfilesize=20;
D.checkext (obj)
}
</script>

<body>
<input name= "" type= "file" onchange= "C (this)"/>

</body>

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.