user782104 user782104 - 5 months ago 36
jQuery Question

jquery validate add rules for an array input

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

Answer

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/

Comments