Jquery-based code for Image Upload and image size verification and image Preview

Source: Internet
Author: User

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 = "";
}
}

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.