Here we will see how to validate check box list control in asp.net. We will use javascript and custom validation to implement this functionality.
The code is as follows:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function CheckboxValidation(sender, args) { var checkBoxList = document.getElementById("<%=chk_list1.ClientID %>"); var checkboxes = checkBoxList.getElementsByTagName("input"); var isValid = false; for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].checked) { isValid = true; break; } } args.IsValid = isValid; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:CheckBoxList runat="server" ID="chk_list1"> <asp:ListItem Text="Example 1" Value="Example 1"></asp:ListItem> <asp:ListItem Text="Example 2" Value="Example 2"></asp:ListItem> <asp:ListItem Text="Example 3" Value="Example 3"></asp:ListItem> <asp:ListItem Text="Example 4" Value="Example 4"></asp:ListItem> <asp:ListItem Text="Example 5" Value="Example 5"></asp:ListItem> <asp:ListItem Text="Example 6" Value="Example 6"></asp:ListItem> </asp:CheckBoxList> <asp:CustomValidator ID="Checkbox_CustomValidator" runat="server" ValidationGroup="CheckBoxValidation" Display="None" ClientValidationFunction="CheckboxValidation" ErrorMessage="One or more checkboxes must be chosen"> </asp:CustomValidator> <asp:Button runat="server" ID="btn_submit" Text="Validate" ValidationGroup="CheckBoxValidation" /> <asp:ValidationSummary runat="server" ID="validateionsummary" ValidationGroup="CheckBoxValidation" ShowMessageBox="true" ShowSummary="false" /> </div> </form> </body> </html>