I would like to store the checkbox value in an array, however, i can not use the validate rules since the name is selectList[] instead of selectList. I tried id but it seems the rule only bind to the name.

html:

<input id='sendList' type='checkbox' name='selectList[]' value='$set[ListID]'>

js rule:

  $("#selectList").validate( {
      rules: {
          selectList[]: {
              required: true,
              minlength: 1
          }
       }
   })

});

Thank you

share|improve this question
up vote 1 down vote accepted

Why not wrap selectList[] inside quotes:

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

In initializing Javascript object property names, they can be an identifier (the way you tried), a number or a string.

Working code: http://jsfiddle.net/rMgLG/

share|improve this answer
1  
Actually this is a non working answer. jQuery validate doesn't allow validation for multiple fields with the same name, so in the above answeronlthe first field with name selectList[] would be validated which doesn't help much if you trying to post an array of fields... – jtheman Mar 12 '13 at 14:22
    
its work for me ..........thanks!!!!!!!!! – Yogesh Singasane Oct 24 '16 at 15:08

Problem is that the readymade jquery.validate.js only validates the first element of category[]. So, we need to modify it a little bit.

In jquery.validate.js, find a function named checkForm, we have to modify it as below:

checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
this.check( this.findByName( elements[i].name )[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
},
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.