views:

1120

answers:

2

Is there a simple way to change the default error values in the jQuery validation plugin?

I just want to rewrite the error messages to be more personal to my app--I have a lot of fields, so I don't want to set the message individually for field x...I know I can do that!

Thanks!

+2  A: 

Add this code in a separate file/script included after the validation plugin to override the messages, edit at will :)

jQuery.extend(jQuery.validator.messages, {
    required: "This field is required.",
    remote: "Please fix this field.",
    email: "Please enter a valid email address.",
    url: "Please enter a valid URL.",
    date: "Please enter a valid date.",
    dateISO: "Please enter a valid date (ISO).",
    number: "Please enter a valid number.",
    digits: "Please enter only digits.",
    creditcard: "Please enter a valid credit card number.",
    equalTo: "Please enter the same value again.",
    accept: "Please enter a value with a valid extension.",
    maxlength: jQuery.validator.format("Please enter no more than {0} characters."),
    minlength: jQuery.validator.format("Please enter at least {0} characters."),
    rangelength: jQuery.validator.format("Please enter a value between {0} and {1} characters long."),
    range: jQuery.validator.format("Please enter a value between {0} and {1}."),
    max: jQuery.validator.format("Please enter a value less than or equal to {0}."),
    min: jQuery.validator.format("Please enter a value greater than or equal to {0}.")
});
Nick Craver
This is really not good practice. Leave the plugin code untouched
jitter
Oh well you changed your answer. I remember a different answer being there before which suggested editing the plugin
jitter
Well whatever let's just remove those comments
jitter
This is good. Is there another way to do it?
Kevin Brown
@Kevin - Not really...you have to at least set the variables, which is all this is doing. You can include this in the page, in another `.js`, whatever method you want to run the script after the plugin to set the messages.
Nick Craver
+1  A: 

Instead of changing the plugin source code you can include an additional js file in the format like those in the downloads localization folder and include that one after loading the validation.js

jQuery.extend(jQuery.validator.messages, {
    required: ...,
    maxlength: jQuery.validator.format(...),
    ...
});
jitter
It isn't. It is technically the same as your answer. I posted my comment to the old version of your answer. Then wrote my own answer. All without continuously reload the page. Thus I only saw you edited answer when checking your comment
jitter