jQuery
--
Questions
--
Followers
Top Experts
Using jQuery validation plugin, need to ensure at least one checkbox is checked when names are different
Hi Experts,
On a web form, I have a set of checkboxes that form a logical group but do not have the same name. Using the jQuery validation plugin (http://docs.jquery.com/Plugins/Validation), I need to add a custom validation method and rule to ensure that the user checks at least one checkbox.
I've read the documentation on jquery.com and understand the basics of adding a method and rule, but I haven't been able to work out the specifics for this particular scenario.
Fair warning :) I already found http://osdir.com/answers/javascript/16692-jquery-validation-plugin-validate-checkboxes-dont-have-samename.html and this does *not* provide the detail I'm looking for. Points won't be given for a reference to this page or anything similar, or for an answer that says, "Write a custom rule." I already figured that part out :) I set the points higher because I'm looking for actual code for the custom method and rule.
Thanks in advance--
Kathryn
On a web form, I have a set of checkboxes that form a logical group but do not have the same name. Using the jQuery validation plugin (http://docs.jquery.com/Plugins/Validation), I need to add a custom validation method and rule to ensure that the user checks at least one checkbox.
I've read the documentation on jquery.com and understand the basics of adding a method and rule, but I haven't been able to work out the specifics for this particular scenario.
Fair warning :) I already found http://osdir.com/answers/javascript/16692-jquery-validation-plugin-validate-checkboxes-dont-have-samename.html and this does *not* provide the detail I'm looking for. Points won't be given for a reference to this page or anything similar, or for an answer that says, "Write a custom rule." I already figured that part out :) I set the points higher because I'm looking for actual code for the custom method and rule.
Thanks in advance--
Kathryn
<input type="checkbox" name="length" value="length">
<input type="checkbox" name="density" value="density">
<input type="checkbox" name="packaging" value="packaging">
<input type="checkbox" name="loose" value="loose">
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
I think this should work for you. You might also want to look into doing custom error positioning, so that only one error shows up, perhaps at the start of the checkbox list, which is also included in the script below.
<script type="text/javascript">
$.validator.methods.checkboxGroup = function(value,element,param)
{
return (
$("input[name='length']").is(':checked') ||
$("input[name='density']").is(':checked') ||
$("input[name='packaging']").is(':checked') ||
$("input[name='loose']").is(':checked')
);
}
$('form').validate({
rules: {
length: "checkboxGroup",
density: "checkboxGroup",
packaging: "checkboxGroup",
loose: "checkboxGroup",
//etc
},
errorPlacement: function(err, el) {
var name = $(el).attr('name');
if (name == 'length' || name=='density' || name=='packaging' || name=='loose')
{
err.insertBefore(el);
}
else
{
err.insertAfter(el);
}
}
});
</script>
anoyes,
YOU ROCK! That's exactly what I was looking for. Thank you!
Just one more detail: it's telling me no error message was defined. Do you know how to add that? (I know how to add a message with validator.addMethod, but the same thing didn't work with validator.methods).
Thank you!
Kathryn
YOU ROCK! That's exactly what I was looking for. Thank you!
Just one more detail: it's telling me no error message was defined. Do you know how to add that? (I know how to add a message with validator.addMethod, but the same thing didn't work with validator.methods).
Thank you!
Kathryn
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
If I could have given you an A+, I would have :) Thanks again!






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
jQuery
--
Questions
--
Followers
Top Experts
jQuery (Core) is a cross-browser JavaScript library that provides abstractions for common client-side tasks such as Document Object Model (DOM) traversal, DOM manipulation, event handling, animation and Ajax. jQuery also provides a platform to create plugins that extend jQuery's capabilities beyond those already provided by the library.