我的验证中有这种情况:
consent: {
required: function(element) {
var age = getAge($("#birthdate").val());
if(age >= 18){
return false;
} else if(age >= 13 && age <=17 ){
$('#consent:checked');
} else {
//should show error message not ligible to register
}
}
}
如何从上述条件返回消息?
最佳答案
我会为每个条件创建一条带有关联消息的特殊规则。对于年龄,必须大于13。对于同意,只有年龄小于18时才需要。
$.validator.addMethod( 'minimumAge', function(value,element) {
return getAge(element) >= value;
});
$('form').validate({
rules: {
age: minimumAge(13),
consent: {
required: function(element) {
return getAge($('#birthDate') < 18;
}
}
}
messages: {
age: "You must be older than 13 to register.",
consent: "If you are under 18 your must have your parent's consent to register."
}
});