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

时间:2009-07-31 10:26:46

标签: jquery jquery-plugins

我在验证中遇到这种情况:

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
        }
    }
}

如何从上述条件返回消息?

1 个答案:

答案 0 :(得分:4)

我会为每个条件创建一个带有关联消息的特殊规则。对于年龄,它必须大于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."
      }
 });