我的验证中有这样的条件:
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."
}
});
关于jquery - 如何在条件jquery验证插件中显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1211808/