Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The showErrors() function of jQuery Validation Plugin

The following script works fine:

$("#regform").validate().showErrors({"username":"message"});

After I changed the script to the below one, it doesn't work.

var name = "username";
$("#regform").validate().showErrors({name:"message"});

I need to pass the field name by a variable. Anyone knows how this problem can be solved?

like image 773
Alan Avatar asked Sep 09 '09 04:09

Alan


1 Answers

You should build an object literal and use the bracket notation member access operator:

var name = "username",
    obj = {};
obj[name] = "message";

$("#regform").validate().showErrors(obj);
like image 67
Christian C. Salvadó Avatar answered Sep 24 '22 22:09

Christian C. Salvadó