gpt4 book ai didi

jquery - 如何在条件jquery验证插件中显示消息

转载 作者:行者123 更新时间:2023-12-01 07:36:25 25 4
gpt4 key购买 nike

我的验证中有这样的条件:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com