Jquery validation with dynamic error message -


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

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -