jQuery Validation using the class instead of the name value

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-05-06T10:21:44Z Indexed on 2010/05/06 10:28 UTC
Read the original article Hit count: 275

Filed under:
|
|
|
|

Hi,

I'd like to validate a form using the jquery validate plugin, but I'm unable to use the 'name' value within the html - as this is a field also used by the server app. Specifically, I need to limit the number of checkboxes checked from a group. (Maximum of 3.) All of the examples I have seen, use the name attribute of each element. What I'd like to do is use the class instead, and then declare a rule for that.

html

This works:

<input class="checkBox" type="checkbox" id="i0000zxthy" name="salutation" value="1" />

This doesn't work, but is what I'm aiming for:

<input class="checkBox" type="checkbox" id="i0000zxthy" name="i0000zxthy" value="1" />

javascript:

var validator = $(".formToValidate").validate({
rules:{
"salutation":{
required:true,
},
"checkBox":{
required:true,
minlength:3 }
}
});

Is it possible to do this - is there a way of targeting the class instead of the name within the rules options? Or do I have to add a custom method?

Cheers, Matt

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about validation