Javascript jquery 验证接受方法 - TypeError:无法读取未定义的属性“调用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33531055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery validate accept method - TypeError: Cannot read property 'call' of undefined
提问by user3927463
I want to check the upload file type using the validate plugin.
我想使用验证插件检查上传文件类型。
But I got the following error message:
但我收到以下错误消息:
Uncaught TypeError: Cannot read property 'call' of undefined.
Exception occurred when checking element file, check the 'accept' method.
Here is my form.
这是我的表格。
<form id="upload" enctype="multipart/form-data">
<div class="control-group">
<div class="controls">
<input id="file" name="file" type="file">
</div>
<div class="form-group">
<button class="btn btn-primary" onclick="Submit()" type="button">Submit</button>
</div>
</div>
</form>
And here is my JavaScript:
这是我的 JavaScript:
var Submit=function(){
var validator = $('#upload').validate({
rules:{
file: {
required: true,
accept: "audio/*, video/*"
}
},
message: {
file:"Invalid file type"
}
});
validator.form();
}
回答by Josh KG
To use the "accept"
method you'll need to include the jQuery Validate additional methods file. Simply include it after your validate file:
要使用该"accept"
方法,您需要包含 jQuery Validate 附加方法文件。只需在验证文件后包含它:
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
Then try initiating your validator like this instead:
然后尝试像这样启动验证器:
$(document).ready(function(){
$("#upload").validate({
...settings...
});
});
And remove the onClick
and type="button"
attribute from your button.
并从您的按钮中删除onClick
andtype="button"
属性。
Here are the docs:
以下是文档:
回答by Sumit Kumar Gupta
I had same issue but I got a solution I have added the below code in script above jquery validation script
我有同样的问题,但我得到了一个解决方案我在 jquery 验证脚本上方的脚本中添加了以下代码
$.validator.addMethod('filesize', function (value, element, param) {
return this.optional(element) || (element.files[0].size <= param)
}, 'File size must be less than {0}');
After that you have to add your own validation script
之后,您必须添加自己的验证脚本
var Submit=function(){
var validator = $('#upload').validate({
rules:{
file: {
required: true,
accept: "audio/*, video/*"
}
},
message: {
file:"Invalid file type"
}
});
validator.form();
}