function:
1. Limit extension: jpg only | | JPG and GIF
2. Limit picture size: K is the unit
3. Limit picture width High: PX as unit (either have, or none)
4. Limit damaged pictures (no preview pictures)
5. Limit the pictures that have changed the extension (such as forcing a dynamic GIF extension to JPG)
Use Restrictions:
To add a onchange event to the Inputfile, make it available in an IMG tag when the file is selected, or use an error
Imglimit.js
Copy Code code as follows:
function limitimg () {
var Img=document.getelementbyid (arguments[0]);//Display the object of the picture
var maxsize=arguments[1];//
var allowgif=arguments[2]| | False
var maxwidth=arguments[3]| | 0;
var maxheight=arguments[4]| | 0;
var postfix=getpostfix (IMG.SRC);
var str= ". jpg";
if (allowgif) {str+= ". gif"}
if (Str.indexof (Postfix.tolowercase ()) ==-1) {
if (allowgif) {return "picture is not well-formed, only JPG or GIF images can be uploaded";} Else{return "Picture format is not correct, can upload jpg image";}
}else if (img.filesize>maxsize*1024) {
Return "picture size exceeds the limit, please limit in" +maxsize+ "K";
}else{
if (img.filesize==-1) {
Return "Picture format error, may be corrupted or change the extension cause, please select a picture again";
}else{
if (maxwidth>0) {
if (img.width>maxwidth) {
Return "The picture width exceeds the limit, please remain in" +maxwidth+ "pixel";
}else{
if (img.height>maxheight) {
Return "The picture height exceeds the limit, please remain in" +maxheight+ "pixel";
}else{
Return "";
}
}
}else{
Return "";
}
}
}
}
Get the file name extension based on the path
function Getpostfix (path) {
Return path.substring (Path.lastindexof ("."), path.length);
}
Page calls:
Copy Code code as follows:
<body>
<input type=" file "onchange=" document.getElementById (' Img1 '). Src=this.value; "/>
<input type=" button "onclick=" AA () "value=" Upload "/> ;
</body>
<mce:script type= "Text/javascript" ><!--
Function aa () {
If limitimg (' Img1 ', 10 0,false,1000,100 = = "") {
alert ("Picture upload succeeded");
}else{
Alert (limitimg (' img1 ', 100,false,1000,100));
}
}
//--></mce:script>