Jquery validation with dynamic error message -
this question has answer here:
i need validate field multiple conditions different error messages. how fix this?
$.validator.addmethod("custommethod", function(value, element) { var msg; if(cond1){ msg = "msg1"; } else if(cond2){ msg = "msg2"; } else if(cond3){ msg = "msg3"; } }, msg);
you have pass in anonymous function in message
parameter, because cannot pass msg
result of previous anonymous function next parameter (as msg
, in context, exists within particular anonymous function scope).
$.validator.addmethod("custommethod", function(value, element) { // usual stuff here. }, function (params, element) { var msg; if(cond1){ msg = "msg1"; } else if(cond2){ msg = "msg2"; } else if(cond3){ msg = "msg3"; } return msg; });
however, declare msg
in global scope this:
var msg; var dynamicerrormsg = function () { return msg; } $.validator.addmethod("custommethod", function(value, element) { if(cond1){ msg = "msg1"; } else if(cond2){ msg = "msg2"; } else if(cond3){ msg = "msg3"; } }, dynamicerrormsg);
and work well.
Comments
Post a Comment