Checkboxlist validation using Custom Validator in ASP.NET

Aspx Code

<asp:CheckBoxList ID="CheckBoxListStates" runat="server" ClientIDMode="Static">
   <asp:ListItem Text="Preliminary Construction" Value="prelim_construction" />
   <asp:ListItem Text="Final Construction" Value="final_construction" />
   <asp:ListItem Text="Construction Alteration" Value="construction_alteration" /> 
   <asp:ListItem Text="Remodel" Value="remodel" />
</asp:CheckBoxList>

<asp:CustomValidator runat="server" ID="cvmodulelist" ClientValidationFunction="ValidateModuleList" Display="Dynamic" ErrorMessage="Please select state">
</asp:CustomValidator>

JavaScript Code

function ValidateModuleList(source, args) {

    var chkListModules = document.getElementById('<%= CheckBoxListStates.ClientID %>');

    var chkListinputs = chkListModules.getElementsByTagName("input");

    for (var i = 0; i < chkListinputs.length; i++) {

        if (chkListinputs[i].checked) {

           args.IsValid = true;

              return;

        }
     }

     args.IsValid = false;

}
Ebook Download
View all
Learn
View all