【问题标题】:how to display message in conditional jquery validation plugin如何在条件 jquery 验证插件中显示消息
【发布时间】:2009-07-31 10:26:45
【问题描述】:

我的验证中有这种情况:

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

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

【问题讨论】:

标签: jquery jquery-plugins


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,但处理年龄的字段是一个文本框,用户可以在其中输入他们的生日。所以用户没有输入他们的年龄我有函数 getAge 来计算他们的年龄。
  • 因此调整代码,使其检查最小年份并根据当前年份和文本框中的日期计算其当前年龄。
  • 谢谢! “依赖:”会导致删除错误,这将使脚本正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
相关资源
最近更新 更多