使用javascript验证图像类型 [英] Validate image type using javascript
本文介绍了使用javascript验证图像类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的表格:
<form method=post src=upload enctype="multipart/form-data">
<input name="img1" id="img1" type="file">
<input type="submit" value="Upload">
</form >
请问如何使用javascript验证此表单,以便只上传jpg文件。谢谢你的阅读。
Please how can I valid this form using javascript so that only jpg files are uploaded. thanks for reading.
推荐答案
你可以绑定你的 onsubmit
事件表格,并检查您的文件输入的值
是否以.jpg或.jpeg结尾,如下所示:
You can bind the onsubmit
event of your form, and check if the value
of your file input ends with ".jpg" or ".jpeg", something like this:
window.onload = function () {
var form = document.getElementById('uploadForm'),
imageInput = document.getElementById('img1');
form.onsubmit = function () {
var isValid = /\.jpe?g$/i.test(imageInput.value);
if (!isValid) {
alert('Only jpg files allowed!');
}
return isValid;
};
};
检查上面的例子这里。
这篇关于使用javascript验证图像类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文