MEM MEM - 2 months ago 14
jQuery Question

Jquery Validation Plugin addMethod with Localization

I read this into jquery website:


message (Optional) String, Function

The default message to display for
this method. Can be a function created
by jQuery.validator.format(value).
When undefined, an already existing
message is used (handy for
localization), otherwise the
field-specific messages have to be
defined.


I have added a method like this:

$.validator.addMethod("postalCode", function(value, element) {
return this.optional(element) || /^[0-9\-]+$/i.test(value);
});


Then, on my localization file I have:

postalCode: jQuery.validator.format("blabla")


However, "blabla" is never assumed.

I've also tried to pass jQuery.format() as a message argument:

$.validator.addMethod("postalCode", function(value, element) {
return this.optional(element) || /^[0-9\-]+$/i.test(value);
}, jQuery.format());


No luck either.

How is this suppose to work?

Thanks a lot,
MEM

MEM MEM
Answer

Something like this:

$.validator.addMethod("postalCode", function(value, element) {
return this.optional(element) || /^[0-9\-]+$/i.test(value);

Should work. If you omit the message, it will get that message from your localization message file.

Great class here. :)

Comments