How do you create rules for jquery form validate plugin with names that are arrays?

Does anyone know how to create rules for a jquery form validate if you have a name attribute that is an array?

eg.

<form id="myForm"> <input type="checkbox" name="data[]" id="firstId" value="1" />One <br /> <input type="checkbox" name="data[]" id="secondId" value="2" />One <br /> 

...

I am trying to attach a rule to this checkbox using validate plugin syntax

 $('#myform').validate({ rules : { data: { required: true, minlength: 1 } } } ); 

The problem is that the syntax for "data" is incorrect. Use of data [] or data \ [\] is also not valid. I just started working with

 $('#firstId').rules('add', { required: true, minlength: 1}); 

Anyone have a suggestion?

+6
jquery plugins validation forms
source share
2 answers

You need to wrap the input name (in this case the data []) in quotation marks

 $('#myform').validate({ rules : { 'data[]': { required: true, minlength: 1 } } }); 

see documentation for a field with complex names (parentheses): http://docs.jquery.com/Plugins/Validation/Reference#Fields_with_complex_names_.28brackets.2C_dots.29

+12
source share

You tried:

 $('#myform').validate({ rules : { 'data[]': { required: true, minlength: 1 } } } ); 

I don't know if this will work, take a picture.

+1
source share

All Articles