views:

1019

answers:

1

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?

+1  A: 

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);
CMS