如何在使用JQuery的输入类型文件中验证文件扩展名? [英] How can the file extension be validated in an input type file using JQuery?
本文介绍了如何在使用JQuery的输入类型文件中验证文件扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< input type =filename =group_documents_fileid =group_documents_fileclass = group-documents-file/>规则:要上传的文件必须具有扩展名JPEG,GIF或PNG,否则警报必须是显示。解决方案请参阅:在文档中通过javascript获取文件上传的文件名,以获取文件名:
<$ ();}();}(){c $ c> var extension = $('#group_documents_file').val()。match(/[^.]+$/)。pop()。toLowerCase();
编辑:
如果您不想使用regEx,我会建议使用split,
var extension = $('#group_documents_file').val().split('。').pop() toLowerCase();
然后,检查允许的扩展名,以下内容:
if(〜$ .inArray(extension,['jpg','jpeg','gif','png '))){
//正确的文件类型...
} else {
//不正确的文件类型...
}
HTML:
<input type="file" name="group_documents_file" id="group_documents_file" class="group-documents-file" />
Rule: The file to upload must have an extension JPEG, GIF or PNG, otherwise an alert must be displayed.
解决方案
See here: get the filename of a fileupload in a document through javascript on how to get the filename:
var extension = $('#group_documents_file').val().match(/[^.]+$/).pop().toLowerCase();
This should get you anything after the period in the filename.
EDIT:
If you don't want to use a regEx, I would recommend using split, with pop:
var extension = $('#group_documents_file').val().split('.').pop().toLowerCase();
Then, to check for allowed extensions, do the following:
if ( ~$.inArray(extension, ['jpg', 'jpeg', 'gif', 'png']) ) {
// correct file type...
} else {
// incorrect file type...
}
这篇关于如何在使用JQuery的输入类型文件中验证文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文