1

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>
4
  • 1
    Start by not using the same ID for all the elements. Then learn about event handlers Commented Aug 7, 2016 at 15:44
  • Do you want to be alerted "Activated" if all the checkboxes are checked? Commented Aug 7, 2016 at 15:50
  • just i want validate if anyone checkbox is actived, for proced to save Commented Aug 7, 2016 at 15:58
  • @adeneo how you say? Commented Aug 7, 2016 at 15:59

2 Answers 2

5
You can validate like below:

function validate(){
    if ($('input[name^=checkboxvar]:checked').length <= 0) {
        alert("Not active");
    }else{
        alert("active");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

very useful when we use checkbox names with array
2

First of all remove the id duplication. Id should be unique per page.

Demo : https://jsfiddle.net/Prakash_Thete/bdjue21w/

If you want to check which checkbox is checked on validate you can just apply common 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.