JQuery验证未获取自定义消息

时间:2017-01-31 13:10:35

标签: jquery jquery-validate

我有以下功能,但是当触发验证时,它会显示库存"此字段是必需的。"消息,而不是我的自定义消息。

     $('#customerEnquiry')
  .validate({ // initialize the plugin
      rules: {
          "InputModel.FirstName":
              {
                  required: true
              },
          "InputModel.Surname": {
              required: true,
              minlength: 3
          },
          messages: {
              "InputModel.FirstName":
                  {
                      required: "Please Supply First Name (or initial)."
                  },
              "InputModel.Surname": {
                  required: "Please Supply Surname.",
                  minlength: "Minimum length of 3."
              }
          }
      }
  });

控制台中没有错误,所以我不知道为什么没有触发消息。我能看到的唯一显而易见的事情是,自动生成的错误标签没有获取测试控件的全名。 (如下图所示)

<label class="error" for="Surname" generated="true">This field is required.</label>

因为我的html是自动生成的,并且数据绑定到我的模型,所以我无法控制输入控件的命名。

那么有什么方法可以解决这个问题,或者我是否需要回过头去与Microsofts进行不显眼的验证,它给了我正确的标签,但是我不想做其他我希望实现的事情

以下是相关HTML的剪辑

<div class="tab-pane active" id="basic-tab">

<div class="control-group">

  <label class="control-label" for="InputModel_FirstName">Customer Forename (or Initial)</label>

    <div class="controls">

      <input name="InputModel.FirstName" class="noltmarg" id="FirstName" type="text" placeholder="Enter First name" value="" data-val-required="First Name is required" data-val="true">

    </div>
</div>
<div class="control-group">
  <label class="control-label" for="InputModel_Surname">Customer Surname</label>

    <div class="controls">
      <input name="InputModel.Surname" class="noltmarg" id="Surname" type="text" placeholder="Enter Surname" value="" data-val-required="Surname is required" data-val="true" data-val-length-min="2" data-val-length-max="30" data-val-length="At Least 2 Letters are Required">

    </div>
</div>

    ... Rest of form omitted for Brevity
</fieldset>
</div>

1 个答案:

答案 0 :(得分:1)

您错放了一个括号,有效地将messages放在rules对象中......

$('#customerEnquiry').validate({ // initialize the plugin
    rules: {
        "InputModel.FirstName": {
            required: true
        },
        "InputModel.Surname": {
            required: true,
            minlength: 3
        },
        messages: {
            "InputModel.FirstName": {
                required: "Please Supply First Name (or initial)."
            },
            "InputModel.Surname": {
                required: "Please Supply Surname.",
                minlength: "Minimum length of 3."
            }
        }
    }
});

相反,messages应该是rules ...

的兄弟姐妹
$('#customerEnquiry').validate({ // initialize the plugin
    rules: {
        // 
    },
    messages: {
        //      
    }
});

DEMO:jsfiddle.net/gLruabce/