How to validate multiple checkbox selection in a JavaScript?

Today, We want to share with you multiple checkbox validation in javascript.In this post we will show you Check if multiple CheckBoxes are checked or not using javascript, hear for At least one checkbox must be selected (checked) we will give you demo and example for implement.In this post, we will learn about how to get multiple checkbox value in javascript? with an example.

Multiple checkbox validation

How to validate a form with multiple checkboxes to have atleast one checked Example.

HTML Code (index.html)

<table id="tblElecProducts">
    <tr>
        <td><input id="chkIphone" type="checkbox" value="1"/><label for="chkIphone">Iphone</label></td>
    </tr>
    <tr>
        <td><input id="chkLaptop" type="checkbox" value="2"/><label for="chkLaptop">Laptop</label></td>
    </tr>
    <tr>
        <td><input id="chkDell" type="checkbox" value="3"/><label for="chkDell">Dell</label></td>
    </tr>
    <tr>
        <td><input id="chkComputer" type="checkbox" value="4"/><label for="chkComputer">Computer</label></td>
    </tr>
    <tr>
        <td><input id="chkPendrive" type="checkbox" value="5"/><label for="chkPendrive">Pendrive</label></td>
    </tr>
</table>
<br />
<input type = "button" value = "Check" onclick = "return Validate()" />

Pure JavaScript Code

<script type="text/javascript">
    function Validate() {
        var selected_data = 0;
 
        //Reference the Table.
        var tblElecProducts = document.getElementById("tblElecProducts");
 
        //Reference all the CheckBoxes in Table.
        var chks = tblElecProducts.getElementsByTagName("INPUT");
 
        //Loop and count the number of checked CheckBoxes.
        for (var i = 0; i < chks.length; i++) {
            if (chks[i].checked) {
                selected_data++;
            }
        }
 
        if (selected_data > 0) {
            alert(selected_data + " CheckBoxe(s) are checked.");
            return true;
        } else {
            alert("Please select CheckBoxe(s).");
            return false;
        }
    };
</script>

I hope you get an idea about dynamic multiple checkboxs validations in javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.