自定义jquery验证所有错误的消息

时间:2011-05-11 15:00:44

标签: jquery jquery-validate

如何为每种错误类型指定自定义错误消息,例如required和date。我想要一个不同的消息,具体取决于错误类型。

我有一个看起来像这样的字段

<input type="text" id="dateOfBirth" name="dateOfBirth" class="required date" />

我的错误消息看起来像这样

This field is requiredPlease enter a valid date

我怎样才能说出以下内容

Date of birth is requiredPlease enter a valid date for Date of Birth


这就是我正在使用的

var validator = $("form:first").validate({
    errorClass: 'Input_State_Error',
    rules: {
        dateOfBirth: "required date"
    },
    messages: {
        dateOfBirth: "Please enter your date of birth"
    },
    // the errorPlacement has to take the table layout into account
    errorPlacement: function(error, element) {                
        error.appendTo($('#ctl00_lblMessage'));
    },
    // specifying a submitHandler prevents the default submit, good for the demo
    submitHandler: function() {
        alert("submitted!");
    },
    // set this class to error-labels to indicate valid fields
    success: function(label) {
        // set &nbsp; as text for IE
        label.html("&nbsp;").addClass("checked");
    }
});

1 个答案:

答案 0 :(得分:3)

关于验证插件的jquery date() documentation,我想你建议:

var validator = $("form:first").validate({
    ...
    messages: {
        dateOfBirth: {
            required: "Date of birth is required",
            date: "Please enter a valid date for Date of Birth"
        }
    }
    ...
});