Jquery code for verifying the size of uploaded images and previewing the images
Upload image Verification
Copy codeThe Code is as follows:
*/
Function submit_upload_picture (){
Var file = $ ('file _ C'). value;
If (! /. (Gif | jpg | jpeg | png | gif | jpg | png) $/. test (file )){
Alert ("Image Watermark watermark is one of .gif, jpeg, jpg, and png ")
} Else {
$ ('Both _ form'). action = "file! Upload. action ";
$ ('Both _ form'). submit ();
$ ('Insert _ img '). sethtml (' ');
$ ('Display _ div '). setstyle ('display', 'block ');
$ ('Upload _ div '). setstyle ('display', 'None ');
}
}
Image type and size verification
Copy codeThe Code is as follows:
// Instance 2
Function validate_edit_logo (){
Var file = $ ('file'). value;
If (! /. (Gif | jpg | jpeg | png | gif | jpg | png) $/. test (file )){
Alert ("Image Watermark watermark is one of .gif, jpeg, jpg, and png ")
If (a = 1 ){
Return false;
}
} Else {
Var image = new image ();
Image. src = file;
Var height = image. height;
Var width = image. width;
Var filesize = image. filesize;
$ ('Beforeend'). src = file;
$ ('Div _ regi_right '). setstyle ('display', 'block ');
If (width> 80 & height> 80 & filesize> 102400 ){
Alert ('upload an image of 80*80 pixels or of a size smaller than 100kb ');
If (a = 1 ){
Return false;
}
}
If (a = 1 ){
Return true;
}
}
}
Image Preview
Copy codeThe Code is as follows:
// Instance 3
Function viewimg (index ){
Var name = 'uploadimg '+ index;
Var imgup = $ (name );
Var imgpath = getpath (imgup );
Var local = imgup. value;
Var point = local. lastindexof (".");
// Determine the size of the uploaded file
Var img = document. createelement ("img ");
Img. src = local;
Var filesize = img. filesize;
Img. onload = function () {filesize = this. filesize ;}
When (img. filesize> 5242880 ){
Alert ("the image file is too large! ");
Return false;
}
// Determine whether the image format is used
Var imgname = imgup. value. substring (imgup. value. lastindexof ("."), imgup. value. length );
Imgname = imgname. tolowercase ();
If (imgname! = ". Jpg") & (imgname! = ". Gif") & (imgname! = ". Jpeg") & (imgname! = ". Png") & (imgname! = ". Bmp ")){
Alert ("u8bf7u9009u62e9u56feu7247u6587u4ef6uff0cu8c22u8c22! ");
Imgup. focus ();
// Clear the value in the file www.3ppt.com
Imgup. select ();
Document. selection. clear ();
} Else {
// Display the image
Document. getelementbyid ("sig_preview" + index ). innerhtml = " ";
}
If (index> = 3 ){
Var cnt = index + 1;
$ ("Img" + cnt). style. display = "";
}
}