0

I'm using the jquery validate plugin and I need to group messages for a group of inputs, but their name values contain a dot and that seems to be a problem. Is there any way to overcome the issue?

    groups: {
        validDateOfBirth: 'firstname.fieldOne firstname.fieldTwo',

    },

Greatly appreciate your help!

Sparky
  • 98,165
  • 25
  • 199
  • 285
Adorablepolak
  • 157
  • 4
  • 16
  • Please show more code. Where is the relevant HTML for the form elements and the rest of the `.validate()` method? – Sparky Jan 29 '15 at 16:58
  • People took time to answer your question. At least you could take the time to respond to them. – Sparky Feb 03 '15 at 17:14

2 Answers2

1

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

Community
  • 1
  • 1
Sparky
  • 98,165
  • 25
  • 199
  • 285
-1

I recommend you to use something different than dot for field name, because you may have problems, see What characters are allowed in the HTML Name attribute inside input tag?

Community
  • 1
  • 1
Alex Barannikov
  • 304
  • 2
  • 8