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?

8
  • 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. Commented Oct 9, 2013 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 Commented Oct 9, 2013 at 18:25
  • Assuming that "test[]" is a valid name, have you tried: $('input [name="test[]"]').each(function() { this.required = true; }); Commented Oct 9, 2013 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. The plugin still requires a unique name on each to keep track of the inputs. Commented Oct 9, 2013 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. Commented Oct 9, 2013 at 18:51

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.