jQuery验证-需要工作,但不能接受 [英] jQuery validation - required working but accept not
问题描述
im上传图片并尝试使用jquery对其进行验证.这是我的代码:
im uploading an image and trying to validate it before with jquery. Here is my code:
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$("#form").validate({
errorLabelContainer: "#message_box", wrapper: "li",
rules: {
image: {required: true, accept: "jpg|jpeg|png|gif"}
},
messages: {
image: {required: 'Required!', accept: 'Not an image!'}
}
})
});
</script>
Required正在工作-如果我未插入任何图像,则会出现错误.但是accept不起作用(我插入的任何东西都通过了),我不知道为什么.有任何想法吗? :)
Required is working - if i insert no image i get an error. But accept isn't working (anything i insert passes) and i cant figure out why. Any ideas? :)
推荐答案
您需要两件事.
(1)使用有效的语法来使用accept
方法,因为它需要您使用逗号分隔的mimetypes列表.
(1) use valid syntax for using accept
method because it requires you to use to provide comma-separated list of mimetypes.
$(document).ready(function(){
$("#form").validate({
errorLabelContainer: "#message_box", wrapper: "li",
rules: {
image: {required: true, accept: "image/jpg,image/jpeg,image/png,image/gif"}
},
messages: {
image: {required: 'Required!', accept: 'Not an image!'}
}
})
});
(2),您必须包括additional-methods.js
,因为accept
方法未包含在核心验证插件中.因此,在包含验证插件
(2) You'll have to include the additional-methods.js
because the accept
methods is not included in the core validate plugin. So add following to your <head>
after you include validate plugin
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script>
这是 链接到jsfiddle .请注意,它包含debug: true
以防止在小提琴中发布表单.
Here's the link to jsfiddle. Note that it includes debug: true
to prevent posting of the form in fiddle.
这篇关于jQuery验证-需要工作,但不能接受的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!