4

I've got multiple inputs with the name test[]

<input type="text" placeholder="" class="m-wrap span12" name="test[]">
<input type="text" placeholder="" class="m-wrap span12" name="test[]">
<input type="text" placeholder="" class="m-wrap span12" name="test[]">

And the following rule:

rules: {
    'test[]': {
        required: true
    }
},

The validation does not work. This is my first time trying to validate an array[] Please, anybody?

mgraph
  • 15,238
  • 4
  • 41
  • 75
EsiX
  • 65
  • 1
  • 6
  • If I recall correctly, jquery validation does not support multiple fields with same name. In your case, I think only the first field will fire the validation. You can either add a counter in the name of the field or add 'required' in input field. The downside of the second solution is that you cannot specify rules for those fields. – aarryy Oct 09 '13 at 18:23
  • without knowing what plugin you are using this is jsut a guess. using [] in the name is invalid, you need to remove the brackets. also If i had to guess, I would say you should be using the class of the element – Daniel Ward Oct 09 '13 at 18:25
  • Assuming that "test[]" is a valid name, have you tried: $('input [name="test[]"]').each(function() { this.required = true; }); – Marcelo Myara Oct 09 '13 at 18:30
  • @aarryy, you are half-correct. Yes, you cannot have multiple fields with the same `name`. However, [using the `required="required"` attribute will not solve anything](http://jsfiddle.net/xG6Xc/). The plugin still requires a unique `name` on each to keep track of the inputs. – Sparky Oct 09 '13 at 18:44
  • @Sparky, I could be wrong as it has been a while since I ran into similar issues. But I do see why people want to keep using same name with empty brackets instead of adding a counter. It's definitely easier to process the posted data on the server side because they will be stored as an array rather than a bunch of variables with similar names. – aarryy Oct 09 '13 at 18:51
  • @aarryy, what can I say... that's just not how this plugin works. – Sparky Oct 09 '13 at 18:54
  • @Sparky, actually in the answer you posted, those input are still stored as an array as those input names are well indexed. It's just sometimes you might need to have dynamic numbers of fields and it's not that easy to handle those counters. – aarryy Oct 09 '13 at 18:57
  • @aarryy, Yes, but I'm not talking about arrays. Point being, when I said, _"that's just not how this plugin works"_, I meant that it cannot handle the same `name` used more than once. – Sparky Oct 09 '13 at 19:08

1 Answers1

5

The jQuery Validate plugin will not allow you to validate multiple input elements with the same name. You must have a unique name on each. There is no workaround; the name attribute is how the plugin internally keeps track of all form inputs.

<input type="text" placeholder="" class="m-wrap span12" name="test[1]">
<input type="text" placeholder="" class="m-wrap span12" name="test[2]">
<input type="text" placeholder="" class="m-wrap span12" name="test[3]">

DEMO: http://jsfiddle.net/CcxZx/

Sparky
  • 98,165
  • 25
  • 199
  • 285