jQuery Validation使用类而不是名称值 [英] jQuery Validation using the class instead of the name value
问题描述
我想使用jquery validate插件来验证表单,但我无法在html中使用'name'值,因为这是服务器应用程序也使用的字段。
具体来说,我需要限制从组中检查的复选框的数量。 (最多3个)我看过的所有示例,使用每个元素的name属性。我想做的是使用类,然后声明一个规则。
I'd like to validate a form using the jquery validate plugin, but I'm unable to use the 'name' value within the html - as this is a field also used by the server app. Specifically, I need to limit the number of checkboxes checked from a group. (Maximum of 3.) All of the examples I have seen, use the name attribute of each element. What I'd like to do is use the class instead, and then declare a rule for that.
html
这样做:
<input class="checkBox" type="checkbox" id="i0000zxthy" name="salutation" value="1" />
这不行,但是我的目标是:
This doesn't work, but is what I'm aiming for:
<input class="checkBox" type="checkbox" id="i0000zxthy" name="i0000zxthy" value="1" />
javascript:
javascript:
var validator = $(".formToValidate").validate({
rules:{
"salutation":{
required:true,
},
"checkBox":{
required:true,
minlength:3 }
}
});
有可能做到这一点 - 是否有一种方法来定位类,而不是规则选项?或者,我必须添加自定义方法吗?
Is it possible to do this - is there a way of targeting the class instead of the name within the rules options? Or do I have to add a custom method?
干杯,
Matt
Cheers, Matt
推荐答案
您可以使用基于该选择器添加规则 .rules(add,options)
,只需根据您的验证选项删除任何您想要的类的规则,然后调用 $(。formToValidate)。validate({...});
,执行:
$(".checkBox").rules("add", {
required:true,
minlength:3
});
这篇关于jQuery Validation使用类而不是名称值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!