Correct, within the groups
option, you cannot use names if they contain special characters. Within the rules
option, you'd surround the names with quotes as per the documentation. But unfortunately, that trick does not work for the groups
option.
However, there is one workaround. You would construct the parameter outside of the .validate()
method and assign it to a variable. Then insert the variable itself within .validate()
.
For your case, I use a jQuery "starts with" selector to grab all input
elements that start with firstname
. You could use something else instead, like a class
, if you want, as long as you only select all elements for this particular grouping.
var names = ""; // create empty string
$('input[name^="firstname"]').each(function() { // grab each input starting w/ 'firstname'
names += $(this).attr('name') + " "; // append each name + single space to string
});
names = $.trim(names); // remove the empty space from the end
$('#myform').validate({
// other options,
groups: {
validDateOfBirth: names // reference the string
}
});
Working DEMO: http://jsfiddle.net/wpzk47t8/
Source: https://stackoverflow.com/a/28029469/594235