【问题标题】:jQueryValidate: Cannot Validate FieldsjQuery Validate:无法验证字段
【发布时间】:2017-01-20 11:56:58
【问题描述】:

我正在努力验证表单,但只有电子邮件地址正在验证。这是我的 sn-p:

    $('#toggle-delivery').click( function() {       
      $("#validate-info").validate({         
        rules: {
    email_address: {
            required: true,
            email: true
          }
phone_number: {
required: true,
digits: true,
minlength: 9
}
    shipping_first_name: {
            required: true,
            minlength: 2
          }
    shipping_last_name: {
            required: true,
            minlength: 2
          }
    shipping_address_1: {
            required: true,
            minlength: 10
          }
        }
      }).form(); 
    });

这可确保仅在单击按钮 #toggle-delivery 时验证表单。到目前为止它运行良好,但电话号码、送货名字、送货地址和送货姓氏未经验证。例如,对于电话号码,虽然我指定只能输入至少 9 个字符的数字,但如果您在其中输入任何内容(即字母),它会自动将 valid 类添加到输入元素中。对于其他三个未验证的字段,您可以插入 1 个字符,即使规则说它应该至少为 10 个。就好像规则会被忽略一样。

我还尝试向每个字段添加自定义错误消息:

jQuery(document).ready(function() {
    jQuery('#validate-info').validate({
onfocusout: function(e) {
            this.element(e);
        },
  messages: {
    email_address: {
      required: "We need your email address to contact you.",
      email: "Oops! This isn't a correct email format. Please check and try again."
    }
}
    });

});

这很好用,但如果我为电话号码添加自定义消息,它就会停止工作。为什么不验证字段?

【问题讨论】:

  • 我可能错了,但你的第一个 sn-p 中的每个规则之间不应该有逗号

标签: jquery validation


【解决方案1】:

尝试在规则之间添加逗号,如下所示:

    $('#toggle-delivery').click( function() {       
  $("#validate-info").validate({         
    rules: {
email_address: {
        required: true,
        email: true
      },
phone_number: {
required: true,
digits: true,
minlength: 9
},
shipping_first_name: {
        required: true,
        minlength: 2
      },
shipping_last_name: {
        required: true,
        minlength: 2
      },
shipping_address_1: {
        required: true,
        minlength: 10
      }
    }
  }).form(); 
});

【讨论】:

  • 我尝试添加逗号,但没有成功。字段未经过验证。
猜你喜欢
  • 2014-03-13
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
相关资源
最近更新 更多