Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validation: change default error message

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!

like image 491
Kevin Brown Avatar asked Mar 16 '10 18:03

Kevin Brown


2 Answers

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}.") }); 
like image 90
Nick Craver Avatar answered Sep 29 '22 09:09

Nick Craver


You can specify your own messages in the validate call. Lifting and abbreviating this code from the Remember the Milk signup form used in the Validation plugin documentation (http://jquery.bassistance.de/validate/demo/milk/), you can easily specify your own messages:

var validator = $("#signupform").validate({     rules: {         firstname: "required",         lastname: "required",         username: {             required: true,             minlength: 2,             remote: "users.php"         }     },     messages: {         firstname: "Enter your firstname",         lastname: "Enter your lastname",         username: {             required: "Enter a username",             minlength: jQuery.format("Enter at least {0} characters"),             remote: jQuery.format("{0} is already in use")         }     } }); 

The complete API for validate(...) : http://jqueryvalidation.org/validate

like image 21
Steven Avatar answered Sep 29 '22 09:09

Steven