I want to validate an Array checkbox using Jquery, but i dont know how validate the checkbox using jquery... this is my code
<div class="col-sm-8">
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='0' id='checkboxvar[]'>L</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='1' id='checkboxvar[]'>M</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='2' id='checkboxvar[]'>Mi</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='3' id='checkboxvar[]'>J</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='4' id='checkboxvar[]'>V</label>
</div>
<script>
function validate() {
if($("#checkboxvar").is(':checked')) {
alert("actived");
} else {
alert("No actived");
}
}
</script>
I want to validate an Array checkbox using Jquery, but i dont know how validate the checkbox using jquery... this is my code
<div class="col-sm-8">
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='0' id='checkboxvar[]'>L</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='1' id='checkboxvar[]'>M</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='2' id='checkboxvar[]'>Mi</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='3' id='checkboxvar[]'>J</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='4' id='checkboxvar[]'>V</label>
</div>
<script>
function validate() {
if($("#checkboxvar").is(':checked')) {
alert("actived");
} else {
alert("No actived");
}
}
</script>
Share
Improve this question
asked Aug 7, 2016 at 15:34
El MickeEl Micke
1512 silver badges13 bronze badges
4
- 1 Start by not using the same ID for all the elements. Then learn about event handlers – adeneo Commented Aug 7, 2016 at 15:44
- Do you want to be alerted "Activated" if all the checkboxes are checked? – Somenath Sinha Commented Aug 7, 2016 at 15:50
- just i want validate if anyone checkbox is actived, for proced to save – El Micke Commented Aug 7, 2016 at 15:58
- @adeneo how you say? – El Micke Commented Aug 7, 2016 at 15:59
2 Answers
Reset to default 5You can validate like below:
function validate(){
if ($('input[name^=checkboxvar]:checked').length <= 0) {
alert("Not active");
}else{
alert("active");
}
}
First of all remove the id duplication. Id should be unique per page.
Demo : https://jsfiddle/Prakash_Thete/bdjue21w/
If you want to check which checkbox is checked on validate you can just apply mon class to all of them and then traverse over it and check if any one of them is checked.
Like below
function validate() {
$(".checkboxvar").each(function(index){
if($(this).is(':checked')){
alert("checked element value : " + + $(this).val());
} else{
alert("unchecked element value: " + $(this).val());
}
});
}
Assuming HTML to be like this :
<div class="col-sm-8">
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='0' class='checkboxvar'>L</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='1' class='checkboxvar'>M</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='2' class='checkboxvar'>Mi</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='3' class='checkboxvar'>J</label>
<label class="checkbox-inline"><input type='checkbox' name='checkboxvar[]' value='4' class='checkboxvar'>V</label>
</div>