I'm using jQuery Validation plugin to validate a form.
The problem is that I can't find a way to validate if a single checkbox in my form is checked
HTML markup:
<label for="terms">terms : </label>
<input type="checkbox" name="terms" id="terms">
jQuery code:
rules: {
terms: "required"
},
messages:{
terms: "check the checbox"
}
Any help would be appreciated.
Maybe your checkbox has css style
display: none
Replace it with
visibility: hidden;
width: 0;
It helped to me.
Example of Checkbox Jquery Validation
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<form id="demo">
<label for="terms">terms : </label>
<input type="checkbox" name="terms[]" value="your value" id="terms">
</form>
Jquery Code
$(document).ready(function() {
$("#demo").validate({
rules: {
'terms[]': { required: true },
},
messages:{
'terms[]': "check the checbox"
}
});
});
HTML markup:
<label for="terms">terms : </label>
<input type="checkbox" name="terms" value="1" id="terms">
jQuery code:
rules: {
terms: {
required : true
}
},
messages:{
terms: {
required : "check the checbox"
}
}
jsfiddle: http://jsfiddle.net/mZ6zJ/
You need to give a value to the checkbox.
<input type="checkbox" name="terms" id="terms" value="accepted">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With