[英]the input file type image size restriction is not working
实际上我尝试使用此URL进行jquery图像文件验证,但是它不起作用。请您验证一下并提出建议吗?
单击此处: https : //github.com/snyderp/jquery.validate.file
<form method="post" enctype="multipart/form-data" id="upload_form">
<input type="file" name="example_file" name="example_file">
<button type="submit">Upload</button>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script>
$(function () {
$("#upload_form")
.validate({
rules: {
example_file: {
fileType: {
types: ["text", "gzip", "zip"]
},
maxFileSize: {
"unit": "KB",
"size": 100
},
minFileSize: {
"unit": "KB",
"size": "10"
}
}
});
});
</script>
您要使用扩展名,因此仅包含jquery和jquery.validate是不够的。 因此,也可以通过下载插件来包含该插件。 (我知道示例中缺少它)
最终解决方案应类似于:
<form method="post" enctype="multipart/form-data" id="upload_form">
<input type="file" name="example_file" name="example_file">
<button type="submit">Upload</button>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script src="jquery.validate.file.js"></script>
<script> $(function () {
$("#upload_form")
.validate({
rules: {
example_file: {
fileType: {
types: ["text", "gzip", "zip"]
},
maxFileSize: {
"unit": "KB",
"size": 100
},
minFileSize: {
"unit": "KB",
"size": "10"
}
}
});
});
</script>
编辑:在jsfiddle中测试uploadfunction并不是最好的主意,因为它一般不允许文件上传。
作为jQuery Validate插件的一部分,没有名为fileType
, maxFileSize
和minFileSize
规则。 您将需要编写自己的规则, 或者可能只是忘了包含引用的来自GitHub的自定义文件上传规则 。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<!--// need to include this one too //-->
<script src="jquery.validate.file.js"></script>
您也缺少右括号...
$("#upload_form").validate({
rules: {
example_file: {
fileType: {
types: ["text", "gzip", "zip"]
},
maxFileSize: {
"unit": "KB",
"size": 100
},
minFileSize: {
"unit": "KB",
"size": "10"
}
} // <- THIS one was missing
}
});
工作演示: http : //jsfiddle.net/h2nduLco/3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.