Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
106
rated 0 times [  107] [ 1]  / answers: 1 / hits: 136924  / 14 Years ago, thu, may 6, 2010, 12:00:00

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


More From » jquery

 Answers
24

You can add the rules based on that selector using .rules(add, options), just remove any rules you want class based out of your validate options, and after calling $(.formToValidate).validate({... });, do this:



$(.checkBox).rules(add, { 
required:true,
minlength:3
});

[#96862] Tuesday, May 4, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lelasamiraa

Total Points: 208
Total Questions: 99
Total Answers: 107

Location: Uzbekistan
Member since Tue, Nov 30, 2021
3 Years ago
;