我有一个包含5个输入字段的表单,我具有以下验证。我正在使用jquery验证插件。
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js"></script>
$("#commentForm").validate({
rules: {
cname1: { required : true, minlength: 2 },
cname2: { required : true, minlength: 2 },
cname3: { required : true, minlength: 2 },
cname4: { required : true, minlength: 2 },
cname5: { required : true, minlength: 2 }
}
});
验证工作正常,没有任何问题。我的要求是,如您所见,所有输入名称均以fwth开头cname
。在这里cname
,我可以定义基于模式的规则,而不是为所有以相同名称开头的字段定义规则吗?类似于$('[name^="cname"]')
?
谢谢!
$("#commentForm").validate();
$('#commentForm [name^="cname"]').each(function() {
$(this).rules('add', { required : true, minlength: 2 });
});
还要检查jQuery.validator.addClassRules以添加您自己的基于类的规则(如.min-2或smth)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句