我正在使用 Jquery 验证程序“http://jqueryvalidation.org/”验证我的表单。
我在验证“<input type="file" />
”字段时遇到问题,它的“required: true,
”验证工作正常,当我接受错误的文件类型时它会抛出错误。但是在“IE 和 Chrome”中,如果我选择了正确的文件格式,它仍然会给出错误,尽管它在 Firefox 中工作正常。
我也创建了 Fiddle ......这就是它对我有效的方式: <强> http://jsfiddle.net/aasthatuteja/v6x8P/
**如果您在 Chrome 和 IE 中 checkin ,它会给出问题,但如果您在 Firefox 中 checkin ,它会工作并给出“已提交”的提示!
请找到下面提到的代码:-
Jquery
<script src="~/Scripts/js/jquery.validate.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>
<script>
$().ready(function () {
// validate signup form on keyup and submit
$("#deploymentUploadForm").validate({
rules:{
File: {
required: true,
accept: "zip"
}
},
messages:{
File: {
required: "This field is mandatory!",
accept: "Accepts only zip file!"
}
}
});
});
</script>
HTML
<form action="~/Deployment/FileUpload" name="deploymentUploadForm" id="deploymentUploadForm" enctype="multipart/form-data" method="post">
<h1>Deployment</h1>
<p>
<input type="file" name="File" accept="application/zip">
</p>
<div role="button" class="marginTop50 marginBottom">
<p>
<input type="submit" id="getDeploymentList" value="Upload" class="active" >
</p>
</div>
</form>
OnSubmit JQUERY
$("#getDeploymentList").click(function () {
if ($("#deploymentUploadForm").valid()) {
$("#deploymentUploadForm").submit();
$('#stepSummary').empty();
$.loader({
className: "blue-with-image",
content: 'Please wait...Your request is being processed!'
});
};
});
如果您需要任何其他信息,请告诉我。
在此先致谢!
最佳答案
尝试使用(HTML):
<input id="fileSelect" type="file" accept=".zip,application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed" />
jQuery(验证)
accept: "application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed"
和 jQuery 插件(想法):
(function( $ )
{
$.fn.acceptFileType=function( types )
{
if ( types == undefined )
{
return true;
}else{
types = types.split(",")
}
this.each(function(){
$( this ).bind("change",function()
{
if( !$.inArray( $( this ).val().replace(/([\d\w.]+)(\.[a-z0-9]+)/i,'\2') , types ) )
{
$( this ).val('');
return false;
}
return true;
});
});
};
})( jQuery );
$( ":file" ).acceptFileType(".zip");
如果您有 PHP,请参阅此 repo
祝你好运!
关于javascript - <input type ="file"/> 文件格式验证在 chrome 和 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18964252/