How to validate CheckBoxList using JavaScript in Asp.net(C#, VB)

checkbox validation using javascript in asp.netIntroduction: In previous articles I explained How to Fill CheckBoxList from Sql Server table in asp.net(C#,VB)  and Fill CheckBoxList based on DropDownList selection in asp.net(C#, VB)  and How to get CheckBoxList selected items in comma separated format in asp.net(C#,VB) and Message box in asp.net website using JavaScript and how to redirect visitor from one website to another website using java script and How to call java script function from code behind file in asp.net 
Now in this article I am going to explain with example how to validate CheckBoxList control i.e. to ensure at least one item is selected in the CheckBoxList. I am using CustomValidator control and JavaScript to validate.

Implementation: Let's understand by an practical example.
  • In the <HEAD> tag of the design page(.aspx) create the JavaScript function to validate the CheckBoxList.
 <script type = "text/javascript">
        function validateCheckBoxList(source, args) {
            var chkListModules = document.getElementById('<%= cblCourses.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;
        }
    </script>
  • In the <Body> tag place a CheckBoxList, a Label control, a Button control and a CustomValidator control. In the CheckBoxList add some items as shown below:
<fieldset style="width:160px">
     <legend>Select your skills</legend> 
        <asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="2">
            <asp:ListItem>Asp.net</asp:ListItem>
            <asp:ListItem>C#</asp:ListItem>
            <asp:ListItem>VB</asp:ListItem>
            <asp:ListItem>WCF</asp:ListItem>
            <asp:ListItem>Jquery</asp:ListItem>
            <asp:ListItem>JavaScript</asp:ListItem>
        </asp:CheckBoxList>
        <asp:Label ID="lblStatus" runat="server" Text=""></asp:Label>
         <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Please select at least one skills." ClientValidationFunction = "validateCheckBoxList" Display="static" ForeColor="Red"></asp:CustomValidator>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit"
            onclick="btnSubmit_Click" />   
  </fieldset>

C#.Net Code to validate CheckBoxList using JavaScript

  • In the code behind file(.aspx.cs) write the code as:
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblStatus.Text="Validation successful";
    }

VB.Net Code to validate CheckBoxList using JavaScript

  • In the code behind file(.aspx.vb) write the code as:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        lblStatus.Text = "Validation successful"
    End Sub

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
Previous
Next Post »

2 comments

Click here for comments
Anonymous
admin
August 16, 2013 ×

You are the best! It worked perfectly.

Reply
avatar
August 16, 2013 ×

It is always nice to hear that my post helped anyone..stay tuned and subscribe for more useful updates...

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..