使用组进行jquery表单验证

时间:2012-04-21 21:04:17

标签: javascript jquery validation

我有一个订单,表格的一部分是地址。它包含公司,街道地址,ZIP,城镇,国家/地区,其中包含所需的每个字段的简单jquery表单验证,以及用于编号的ZIP验证。现在我想要的是对它们进行分组,并且只有一个字段显示"有效地址"成功,或者#34;解决错误"出错了。

这是我的js代码的一部分:

$("#pageform").validate(
{
   messages: {
      company_from:     addressError, //addressError is a JS var for the error message
      street_from:      addressError,
      zip_from:         addressError,
      town_from:        addressError,
      country_from:     addressError
   },
   groups: {
      from:    "company_from  street_from  zip_from  town_from  country_from"
   },
   errorPlacement: function(error, element) {
      if (element.attr("name") == "company_from"
         || element.attr("name") == "street_from"
         || element.attr("name") == "zip_from"
         || element.attr("name") == "town_from"
         || element.attr("name") == "country_from"
         )
      {
         $("#error_from").append(error);
      }
   },
   success: function(label) {
      var attribute = $(label[0]).attr("for");
      $(".err-ok-" + attribute + " .ok").css("display", "block").css("visibility", "visible");
   }
}
);

这是相应HTML代码的一部分:

<input type="text" name="company_from" id="company_from" class="required default input-s8" maxlength="255" />
<input type="text" name="street_from" id="street_from" class="required default input-s8" maxlength="255" />
<input type="text" name="zip_from" id="zip_from" class="required digits default input-s8" maxlength="5" onblur="checkCalculation()" />
<input type="text" name="town_from" id="town_from" class="required default input-s8" maxlength="255" />
<!-- and a select list for the country -->

您不需要仔细查看我如何显示错误等等,我的问题是,我不知道何时显示错误标签以及何时成功标签。当我输入邮政编码的字母时,我的errorPlacement函数和成功函数被调用(并且首先是errorPlacement),所以我想如果至少有一个字段是正确的,它总是调用成功。

请询问是否有任何问题,我很确定有......: - )

1 个答案:

答案 0 :(得分:0)

添加规则

    rules: {
     company_from: "required",
     company_from: "required",
     zip_from: "required",
     town_from:"required",
     country_from:"required"
    },
相关问题