Validate CheckBoxList: All CheckBoxes checked using JavaScript in ASP.Net

Last Reply 5 months ago By arunkurmi

Posted 5 months ago

hello,

I have checkboxlist in which it has 3 listitem on button click event i want to make sure that user has selected all 3 items javascript.

thanks

Posted 5 months ago Modified on 5 months ago

Hi nauna,

I have created a sample. please take its reference.

HTML

<div>
    <script type="text/javascript">
        function CheckAll() {
            var table = document.getElementById("cblFruits");
            var checkboxes = table.getElementsByTagName('INPUT');
            for (var i = 0; i < checkboxes.length; i++) {
                if (!checkboxes[i].checked) {
                    alert('Check all 3 CheckBoxes.');
                    break;
                }
            }
        }
    </script>
    <asp:CheckBoxList runat="server" ID="cblFruits">
        <asp:ListItem Value="Apple" Text="Apple" />
        <asp:ListItem Value="Orange" Text="Orange" />
        <asp:ListItem Value="Grapes" Text="Grapes" />
    </asp:CheckBoxList>
    <br />
    <asp:Button Text="Save" runat="server" ID="btnSave" OnClientClick="CheckAll();" />
</div>

Screenshot