【问题标题】:JQuery form validation - validate form based on other form fieldJQuery 表单验证 - 根据其他表单字段验证表单
【发布时间】:2010-07-25 00:00:27
【问题描述】:

我正在使用 Jquery 表单验证,其基本表单如下所示

$('#shopping_form').validate({          
  submitHandler: function(){
  $.post("/url", { message: messageval}, function(data) {
  ......
  ......
  },
}
,rules: {
    message: { required:true },
    price: { number: true  }                
}
});

现在我在这个表单中添加了两个单选按钮,分别称为 radio1 和 radio2。 如果选择了 radio1,则字段消息是必需的,而对于 radio2,它不是必需的字段。

如何添加这种规则。 提前感谢您的帮助

【问题讨论】:

    标签: jquery jquery-plugins jquery-validate jquery-forms-plugin


    【解决方案1】:

    您可以通过编写一个自定义方法来检查单选按钮的值,并根据该值检查消息字段。这是您可以在 jQuery 中添加的代码

    $.Validation.addRule("message",{
     check: function(value) {
        // Check the value of radio button here, You can do it as 
          if($("form input:radio:checked").val() == 'radio2') return true;
         // radio2 is checked then return valid, ie. this field is not required field.
        if(value.length > 0) {
            return true;
        }
        return false;
    },
    msg : "Field is required."
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多