I have something like this:
<form>
<input name='roles' type='checkbox' value='1' />
<input name='roles' type='checkbox' value='2' />
<input name='roles' type='checkbox' value='3' />
<input name='roles' type='checkbox' value='4' />
<input name='roles' type='checkbox' value='5' />
<input type='submit' value='submit' />
<form>
I would like to validate that at least one checkbox (roles) should be checked, is it possible with jquery.validate ?
This question is related to
javascript
jquery
jquery-validate
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();
}
});
});
$("#idform").validate({
rules: {
'roles': {
required: true,
},
},
messages: {
'roles': {
required: "One Option please",
},
}
});
if ($('input:checkbox').filter(':checked').length < 1){
alert("Check at least one!");
return false;
}
It's highly probable that you want to have a text next to the checkbox. In that case, you can put the checkbox inside a label like I do below:
<label style="width: 150px;"><input type="checkbox" name="damageTypeItems" value="72" aria-required="true" class="error"> All Over</label>
<label style="width: 150px;"><input type="checkbox" name="damageTypeItems" value="73" aria-required="true" class="error"> All Over X2</label>
The problem is that when the error message is displayed, it's going to be inserted after the checkbox but before the text, making it unreadable. In order to fix that, I changed the error placement function:
if (element.is(":checkbox")) {
error.insertAfter(element.parent().parent());
}
else {
error.insertAfter(element);
}
It depends on your layout but what I did is to have a special error placement for checkbox controls. I get the parent of the checkbox, which is a label, and then I get the parent of it, which is a div in my case. This way, the error is placed below the list of checkbox controls.
Mmm first your id attributes must be unique, your code is likely to be
<form>
<input class='roles' name='roles' type='checkbox' value='1' />
<input class='roles' name='roles' type='checkbox' value='2' />
<input class='roles' name='roles' type='checkbox' value='3' />
<input class='roles' name='roles' type='checkbox' value='4' />
<input class='roles' name='roles' type='checkbox' value='5' />
<input type='submit' value='submit' />
</form>
For your problem :
if($('.roles:checkbox:checked').length == 0)
// no checkbox checked, do something...
else
// at least one checkbox checked...
BUT, remember that a JavaScript form validation is only indicative, all validations MUST be done server-side.
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
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
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).
Source: Stackoverflow.com