admin管理员组文章数量:1323503
I want to check wheather the user uploaded the file or not before clicking submit button through JavaScript/jQuery.
If a file is not uploaded, I don't want to allow form submission.
My html is:
<form action="/home/save" method="post" enctype="multipart/form-data" >
Upload Image: <input type="file" name="image" />
<input type="submit" value="upload" />
</form>
I want to check wheather the user uploaded the file or not before clicking submit button through JavaScript/jQuery.
If a file is not uploaded, I don't want to allow form submission.
My html is:
<form action="/home/save" method="post" enctype="multipart/form-data" >
Upload Image: <input type="file" name="image" />
<input type="submit" value="upload" />
</form>
Share
edited Jun 1, 2013 at 22:22
dsgriffin
68.6k17 gold badges140 silver badges138 bronze badges
asked Jun 1, 2013 at 22:19
Sarmad parvaizSarmad parvaiz
613 silver badges8 bronze badges
2
- 3 This is actually a bad design. Why would you upload a file even the form is not plete? – batbaatar Commented Jun 1, 2013 at 22:25
- The required attribute is not widely supported. – Ray Nicholus Commented Jun 1, 2013 at 22:36
1 Answer
Reset to default 7Using jQuery:
if(!$('input[type="file"]').val()) {
// No file is uploaded, do not submit.
return false;
}
jsFiddle.
Using the HTML5 required
attribute (note: only supported in Chrome, Firefox, Opera, Safari 5 (on Mac) and IE10):
<input type="file" name="image" required/>
jsFiddle.
本文标签: javascriptCheck if file is uploaded before form submissionStack Overflow
版权声明:本文标题:javascript - Check if file is uploaded before form submission - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742141498a2422599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论