您好我正在使用jquery版本1.6.2和jquery验证插件1.5.2,
我有自定义验证器
jQuery.validator.addMethod("venue_type", function(value, element) {
if(value>0){return true;}else{return false;}
}, "venue type is required");
选中下拉值。
我有一个名为venue_edit_form
<form id="venue_edit_form" class="venue_edit_form"
directory_id="<?php echo $venue_details->DirectoryID; ?>">
//code
</form>
我已在此表单中添加了jquery验证
jQuery('#venue_edit_form').validate({
rules:{
venueName:{
required:true,
minlength:1,
maxlength:50
},
venueDescription :{
required:false,
lettersonly:true,
maxlength:150
}
},
messages:{
venueName: {
required:"Venue name is required",
minlength:"Minimum 1 character required",
maxlength:"Venue name should not exceed 50 characters"
},
venueDescription:{
maxlength:"Description should not exceed 150 characters"
}
}
});
我只验证场地名称和描述,但是当我验证表格时,这个奇怪的东西会附加。
错误即将来临,但我没有在编辑表单中验证下拉菜单, 然后我检查来源
保存按钮中有一个名为venue_type
的自定义属性我很惊讶,我更改了自定义验证名称
jQuery.validator.addMethod("venue_type_validater", function(value, element) {
if(value>0){return true;}else{return false;}
}, "venue type is required");
现在它工作正常,
为什么自定义属性出错, 我认为jquery验证插件通过字段名称验证。
这是一个错误????
请告诉我发生了什么。
提前感谢。
答案 0 :(得分:1)
如果从验证插件中查看以下代码,它也会使用attributeRules
var data = $.validator.normalizeRules(
$.extend(
{},
$.validator.classRules(element),
$.validator.attributeRules(element),
$.validator.dataRules(element),
$.validator.staticRules(element)
), element);
在attributeRules中,如果元素中的属性与规则名称匹配,则规则将应用于元素
尝试
jQuery('#venue_edit_form').validate({
ignore: ':hidden, #venue_edit_save',
rules:{
venueName:{
required:true,
minlength:1,
maxlength:50
},
venueDescription :{
required:false,
lettersonly:true,
maxlength:150
}
},
messages:{
venueName: {
required:"Venue name is required",
minlength:"Minimum 1 character required",
maxlength:"Venue name should not exceed 50 characters"
},
venueDescription:{
maxlength:"Description should not exceed 150 characters"
}
}
});