user1032531 user1032531 - 5 years ago 228
Javascript Question

Modifing jQuery validation message

How can I change the jQuery validation message based on logic used when validating? My attempt is shown below, however,

this.extra
shows up as being undefined.

$.validator.addMethod("someMethod", function(value, element, params) {
//Logic to validate and determine message goes here...
var valid=false;
this.extra="bla";
return valid;
}, $.validator.format("message plus "+this.extra)
);

Answer Source

Yes you can do that, but it's bit tricky. You need to set attribute or use .data to associate your information with element.

$.validator.addMethod("someMethod", function(value, element, params) {
    //Logic to validate and determine message goes here...
    var valid=false;
    $(element).data('extra', "bla");
    return valid;
    }, function(params, element) {
      return $.validator.format("message plus " + $(element).data('extra'))
    }
);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download