1

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

3 Answers 3

1

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. :)

Sign up to request clarification or add additional context in comments.

1 Comment

I searched for more complicated way and you have the soluce ... so simple. Thanks.
0

In method $.validator.addMethod() the last optional param is a default constant message as text in quots. You have to use jQuery.validator.format("... {0}...{1}...") instead in case the defined handler has parameters:

addMethod("methodName", function(value, element, params){

which can be outputed in error message, replacing the mask "{N}" (here is why function "format" needed for), where N is the number of parameter if there are several of them passed. For i.g. rule "minlength:10" means that the "10" is a value of the third variable in handler func.

If you do not specify a default message, then it can be defined in section "messages":

,messages : {
   someField1 : {methodName:"error message"},
   someField2 : {methodName:$.validator.messages.custom_mess}
}

It is desired to include js code in the following sequence:

  1. "validate" plugin
  2. "addMethod" instructions
  3. "localization" file
  4. your onLoad code with $('#someForm').validate({...})

Comments

0

If you want e.g. "customPassword" validation.

  1. Make .js files whit locale code on end (messages_en.js, messages_de.js).
  2. Include file depending of chosen app language.
  3. Add translation messages like this:

$.extend( $.validator.messages, {
	customPassword: "One number, small and big letter and min 8 chars.",
	required: "This field is required.",
	remote: "Please fix this field."
});

  1. Then add method whit second parameter "$.validator.messages.customPassword" instead of string message.

jQuery.validator.addMethod("customPassword", function(value, element, params){
    return this.optional(element) || (value.match(/((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,50})/));
}, $.validator.messages.customPassword);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.