I have something like this:
make sure the input-name[]
is in inverted commas in the ruleset. Took me hours to figure that part out.
$('#testform').validate({
rules : {
"name[]": { required: true, minlength: 1 }
}
});
read more here... http://docs.jquery.com/Plugins/Valid...ets.2C_dots.29
The validate plugin will only validate the current/focused element.Therefore you will need to add a custom rule to the validator to validate all the checkboxes. Similar to the answer above.
$.validator.addMethod("roles", function(value, elem, param) {
return $(".roles:checkbox:checked").length > 0;
},"You must select at least one!");
And on the element:
<input class='{roles: true}' name='roles' type='checkbox' value='1' />
In addition, you will likely find the error message display, not quite sufficient. Only 1 checkbox is highlighted and only 1 message displayed. If you click another separate checkbox, which then returns a valid for the second checkbox, the original one is still marked as invalid, and the error message is still displayed, despite the form being valid. I have always resorted to just displaying and hiding the errors myself in this case.The validator then only takes care of not submitting the form.
The other option you have is to write a function that will change the value of a hidden input to be "valid" on the click of a checkbox and then attach the validation rule to the hidden element. This will only validate in the onSubmit event though, but will display and hide messages at the appropriate times. Those are about the only options that you can use with the validate plugin.
Hope that helps!
This is how I have dealt with it in the past:
$().ready(function() {
$("#contact").validate({
submitHandler: function(form) {
// see if selectone is even being used
var boxes = $('.selectone:checkbox');
if(boxes.length > 0) {
if( $('.selectone:checkbox:checked').length < 1) {
alert('Please select at least one checkbox');
boxes[0].focus();
return false;
}
}
form.submit();
}
});
});
Example from https://github.com/ffmike/jquery-validate
<label for="spam_email">
<input type="checkbox" class="checkbox" id="spam_email" value="email" name="spam[]" validate="required:true, minlength:2" /> Spam via E-Mail </label>
<label for="spam_phone">
<input type="checkbox" class="checkbox" id="spam_phone" value="phone" name="spam[]" /> Spam via Phone </label>
<label for="spam_mail">
<input type="checkbox" class="checkbox" id="spam_mail" value="mail" name="spam[]" /> Spam via Mail </label>
<label for="spam[]" class="error">Please select at least two types of spam.</label>
The same without field "validate" in tags only using javascript:
$("#testform").validate({
rules: {
"spam[]": {
required: true,
minlength: 1
}
},
messages: {
"spam[]": "Please select at least two types of spam."
}
});
And if you need different names for inputs, you can use somethig like this:
<input type="hidden" name="spam" id="spam"/>
<label for="spam_phone">
<input type="checkbox" class="checkbox" id="spam_phone" value="phone" name="spam_phone" /> Spam via Phone</label>
<label for="spam_mail">
<input type="checkbox" class="checkbox" id="spam_mail" value="mail" name="spam_mail" /> Spam via Mail </label>
Javascript:
$("#testform").validate({
rules: {
spam: {
required: function (element) {
var boxes = $('.checkbox');
if (boxes.filter(':checked').length == 0) {
return true;
}
return false;
},
minlength: 1
}
},
messages: {
spam: "Please select at least two types of spam."
}
});
I have added hidden input before inputs and setting it to "required" if there is no selected checkboxes
if ($('input:checkbox').filter(':checked').length < 1){
alert("Check at least one!");
return false;
}
sir_neanderthal has already posted a nice answer.
I just want to point out that if you would like to use different names for your inputs you can also use (or just copy the method) require_from_group method from official jQuery Validation additional-methods.js (link to CDN for version 1.13.1).