[jQuery] [validate] changing messages to built-in rules in the jQuery validation plugin
Hi,
I'm using the jQuery validation plugin, and I'd like to change the
message for required fields from "This field is required." to, say,
"fine moon cheese!".
I don't want to apply this to a specific input element (I can use its
'name' for that).
I also I don't mind adding a new validation method for this.
I found a few solutions to this but I don't like them much, is there
something more elegant?
Solution 1:
$.validator.messages["required"] = "Fine moon cheese!";
But that affects every form on the page, and I'm not sure it's
documented or supported. Not good.
Solution 2:
$.validator.addMethod(
"cheeseRequired",
function(name, element) { return !this.optional(element); },
"Fine moon cheese!"
);
Now I can use 'cheeseRequired' and get my custom message, but still
not good:
(1) I couldn't find a supported way to use existing validation
methods in a new one
(2) I couldn't find this use of 'optional' documented anywhere either
(I looked at additional-methods.js and at optional's source to
understand what it does). Is it supported?
(3) How do I do the same for other validations, like minlength?
Thanks!
orip