FarFigNewton
FarFigNewton

Reputation: 7273

Custom jquery validate messages for all errors

How can I specify a custom error message for each error type, like required and date. I want a different message depending on the error type.

I have a field that looks like this

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

my error messages look like this

This field is required or Please enter a valid date

How can I make it say the following

Date of birth is required and Please enter a valid date for Date of Birth


This is what I'm using

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");
    }
});

Upvotes: 2

Views: 1704

Answers (1)

I.G. Pascual
I.G. Pascual

Reputation: 5965

Following jquery date() documentation for Validation Plugin, i guess you suggest this:

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

Upvotes: 3

Related Questions