Multiple upload files use js to verify the file format and size (recommended ).

Source: Internet
Author: User

Multiple upload files use js to verify the file format and size (recommended ).

Html section:

<Dsp: form action = "$ {originatingRequest. requestURI} "method =" post "enctype =" multipart/form-data "> <dsp: input type =" file "bean =" ReturngoodsFormHandler. uploadedFile1 "id =" uploadedFile1 "value =" "onchange =" checkfile ('uploadedfile1'); "/> <dsp: input type =" file "bean =" ReturngoodsFormHandler. uploadedFile2 "id =" uploadedFile2 "value =" "onchange =" checkfile ('uploadedfile2'); "/> <a class =" btn_sprite btn_font14_red "href =" javascript :; "rel =" external nofollow "> <span> submit an application </span> </a> <dsp: input type =" submit "style =" display: none; "bean =" ReturngoodsFormHandler. submitReturngoods "id =" submitReturngoods "value =" Add "/> </dsp: form>

Note: The Upload file must have an id, and an id name should be added to the onchage event;

Js section:

// Detaches verification settings and error messages, facilitating the final submission of var flag = true; var errorinfo = ""; // verifies the file format function checkfile (filenames) {filename = document. getElementByIdx_x_x (filenames ). value; var re = /(. [.] bmp) $ | (. [.] gif) $ | (. [.] jpg) $ | (. [.] png) $ | (. [.] jpeg) $/I; if (! Re. test (filename) {errorinfo = "only bmp, gif, jpg, png, and jpeg files are supported. upload files again"; alert (errorinfo); flag = false; return false;} // verify the file size if (document. getElementByIdx_x_x (filenames ). size> 8000) {errorinfo = "the file must be smaller than kb, and the image size is too large:" + document. getelementbyidx_x_x_x_x (filenames ). size; alert (errorinfo); return false ;}} jQuery ('. btn_font14_red '). click (function () {if (flag = true) {$ ("# submitReturngoods "). click () ;}else {alert (errorinfo); return false ;}});

Note:The advantage of doing so is: first, if an error occurs during file upload, the user is notified immediately and the user can modify it;

Second, if the user has completed the last filling, even though there is a prompt statement, but the user forgot what it is, when clicking submit, the user can still be prompted for the cause of the problem. At the same time, you can save page efficiency by placing the error message and setting value on the outermost side without further verification.

(Document. getElementByIdx_x_x (filenames) does not know why multiple _ x files are always used for posting, but they cannot be removed. Remember to remove them when copying files)

The above method for verifying the format and size of multiple uploaded files using js (recommended) is all the content shared by Alibaba Cloud xiaobian. I hope to give you a reference, we also hope that you can support the customer's home.

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.