我是靠谱客的博主 优秀板凳,这篇文章主要介绍多个上传文件用js验证文件的格式和大小的方法(推荐),现在分享给大家,希望可以做个参考。

html部分:

复制代码
1
2
3
4
5
6
7
8
<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>提交申请</span></a> <dsp:input type="submit" style="display:none;" bean="ReturngoodsFormHandler.submitReturngoods" id="submitReturngoods" value="Add"/> </dsp:form>

需要注意:上传文件要有一个id,然后onchage事件里加一个id的名称;

js部分:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//将验证的设置和错误信息抽离出来,有利于最后submit时候做判断 var flag=true; var errorinfo=""; //验证文件的格式 function checkfile(filenames){ filename=document.getElementByIdx_x_x(filenames).value; var re=/(.[.]bmp)$|(.[.]gif)$|(.[.]jpg)$|(.[.]png)$|(.[.]jpeg)$/i; if (!re.test(filename)) { errorinfo = "只支持bmp,gif,jpg,png,jpeg格式文件,请重新上传"; alert(errorinfo); flag = false; return false; } //验证文件的大小 if(document.getElementByIdx_x_x(filenames).size>8000){ errorinfo = "文件必须小于800KB,图片太大 size:"+document.getElementByIdx_x_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; } });

注意:这么做的好处是:第一:上传文件的时候,如果有错误,第一时间通知用户,用户去修改;

第二:如果用户最后填完了,虽然之前有提示语句,但是用户忘了是什么,点击提交的时候,仍然可以提示用户问题的原因。同时将错误信息和设置值放在最外面,而不用再次验证,可以节省页面效率。

(document.getElementByIdx_x_x(filenames) 不知道为啥发表的时候,总是多个_x,去又去不掉,大家在copy的时候记得去掉哈)

以上这篇多个上传文件用js验证文件的格式和大小的方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

最后

以上就是优秀板凳最近收集整理的关于多个上传文件用js验证文件的格式和大小的方法(推荐)的全部内容,更多相关多个上传文件用js验证文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(135)

评论列表共有 0 条评论

立即
投稿
返回
顶部