Client Side Javascript CheckBoxList Validation using Custom Validator in Asp.Net C# VB.Net

Regarding Custom Validator Control I have talked several times that in many cases we can not validate some ASP.Net controls using built in Validators. CheckBoxList Control is one of them. We can not assign the Checkboxlist control as a ControlToValidate property of RequiredValidator. That’s why Custom Validator is a must required in this scenario. In this article I will explain how to validate CheckBoxList at client side to make sure that at least one item is checked using a Javascript function as a clientvalidationfunction property of Custom Validator Control.

To do the Example copy the below HTML Code:

    <asp:CheckBoxList ID="CheckBoxList1" runat="server">
        <asp:listitem text="January" value="1"></asp:listitem>
        <asp:listitem text="February" value="2"></asp:listitem>
        <asp:listitem text="March" value="3"></asp:listitem>
        <asp:listitem text="April" value="4"></asp:listitem>
        <asp:listitem text="May" value="5"></asp:listitem>
        <asp:listitem text="June" value="6"></asp:listitem>
        <asp:listitem text="July" value="7"></asp:listitem>
        <asp:listitem text="August" value="8"></asp:listitem>
        <asp:listitem text="September" value="9"></asp:listitem>
        <asp:listitem text="October" value="10"></asp:listitem>
        <asp:listitem text="November" value="11"></asp:listitem>
        <asp:listitem text="December" value="12"></asp:listitem>
    </asp:CheckBoxList><br />
    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please Select a Month !!" ForeColor="Red" Font-Bold="true" clientvalidationfunction="ValidateMonth" ></asp:CustomValidator>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Validate Month" />

Now write the Javascript Function to use in CustomValidator:

    <script type="text/javascript">
        function ValidateMonth(source, args) {
            var ControlRef = document.getElementById('<%= CheckBoxList1.ClientID %>');
            var CheckBoxListArray = ControlRef.getElementsByTagName('input');
            var bValid = 0;

            for (var i = 0; i < CheckBoxListArray.length; i++) {
                if (CheckBoxListArray[i].checked == true) {
                    bValid = 1;
            args.IsValid = Boolean(bValid);

Client Side CheckBoxList Validation using Custom Validator in Asp.Net Csharp VB.Net

Hope now you can validate CheckBoxList as a Required parameter in your page.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, Client, Javascript, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *