Click here to Skip to main content
15,884,628 members
Articles / Web Development / ASP.NET

ASP.NET CheckBoxList Client Side Validation using JavaScript

24 Feb 2014CPOL1 min read 46.7K   16   7
In this blog, we will explore a trick to validate whether any CheckBox inside one CheckBoxList is checked or not.

ASP.NET CheckBox Validation Nothing Checked

ASP.NET CheckBox Validation Nothing Checked

ASP.NET CheckBox Validation Atleast one Checked

ASP.NET CheckBox Validation Atleast one Checked

Introduction

In this blog, we will explore a trick to validate whether any CheckBox inside one CheckBoxList is checked or not.

Problem

When you define one CheckBoxList on aspx page by writing code something like below…

ASP.NET
<asp:CheckBoxList RepeatDirection="Horizontal" 
                  ID="chkDemo"
                  runat="server">
    <asp:ListItem Text="Apples" Value="1"></asp:ListItem>
    <asp:ListItem Text="Oranges" Value="2"></asp:ListItem>
    <asp:ListItem Text="Mangoes" Value="3"></asp:ListItem>
</asp:CheckBoxList>

...it will render on browser like below…

HTML
<table id="chkDemo">
    <tbody>
        <tr>
            <td>
                <input type="checkbox" value="1" name="chkDemo$0" id="chkDemo_0">
                <label for="chkDemo_0">Apples</label>
            </td>
            <td>
                <input type="checkbox" value="2" name="chkDemo$1" id="chkDemo_1">
                <label for="chkDemo_1">Oranges</label>
            </td>
            <td>
                <input type="checkbox" value="3" name="chkDemo$2" id="chkDemo_2">
                <label for="chkDemo_2">Mangoes</label>
            </td>
        </tr>
    </tbody>
</table>

Basically, it renders number of CheckBoxes depending on the number of ListItems inside CheckBoxList.

So, What Is the Logic Here?

We will call one JavaScript function on a Button Click.
Button would look like…

ASP.NET
<asp:Button runat="server" ID="Button1" Text="Submit" 
            OnClientClick="return validateCheckBoxList();" />

Inside that function, we will run the following logic to validate whether any CheckBox is checked or not.

  1. We will find the main CheckBoxList first, which is rendered as a Table.
  2. Next, we need to find all the CheckBoxes inside that Table.
  3. After that, we have to check if any CheckBox is checked by looping through them.
  4. If any CheckBox is checked, then break the Loop and show alert (for demo purpose).
  5. Return true if any CheckBox is checked, else show alert and return false.
JavaScript
function validateCheckBoxList() {
    var isAnyCheckBoxChecked = false;

    // ::: Step-1 & 2 ::: Let's get all the CheckBoxes inside the CheckBoxList.
    var checkBoxes = document.getElementById("chkDemo").getElementsByTagName("input");
    
    // ::: NOTE ::: For jsfiddle demo I have directly used the ID. 
    // Otherwise you might have to use ClientID like below...
    // document.getElementById
    // ("<%= chkDemo.ClientID %>").getElementsByTagName("input");

    // ::: Step-3 ::: Now let's Loop through the Children.
    for (var i = 0; i < checkBoxes.length; i++) {
        if (checkBoxes[i].type == "checkbox") {
            if (checkBoxes[i].checked) {
                // ::: Step-4 ::: If current CheckBox is checked, then show alert.
                // Break the Loop.
                isAnyCheckBoxChecked = true;
                alert("Atleast one CheckBox is checked");
                break;
            }
        }
    }
 
    // ::: Step-5 ::: Check if any CheckBox is checked or not.
    // Show alert and return accordingly.
    if (!isAnyCheckBoxChecked) {
        alert("No CheckBox is Checked.");
    }

    return isAnyCheckBoxChecked;
}

See the Demo

Here

Note

I have used the CheckBoxList ID directly, which is chkDemo. But when your ClientID changes, you can get the CheckBoxList like…

JavaScript
document.getElementById("<%= chkDemo.ClientID %>");

Do You Find It Interesting?

Share your thoughts on the blog. Don’t forget to like and share.

Image 3 Image 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Proud Indian | Author | TEDx Speaker | Microsoft MVP | CodeProject MVP | Speaker | DZone Most Valuable Blogger| jsfiddler

My Website

taditdash.com

Programming Community Profiles

jsfiddle | Stack Overflow

Social Profiles

Facebook | Twitter | LinkedIn

Awards


  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015, 2016, 2017, 2018
  3. Code Project MVP 2014, 2015, 2016
  4. Star Achiever of the Month December 2013
  5. Mindfire Techno Idea Contest 2013 Winner
  6. Star of the Month July 2013

Comments and Discussions

 
GeneralNice one Pin
Member 1338498229-Aug-17 21:03
Member 1338498229-Aug-17 21:03 
QuestionGood explained dude!,,, My vote for 5 ! Pin
Sujee116-Feb-14 5:22
Sujee116-Feb-14 5:22 
AnswerRe: Good explained dude!,,, My vote for 5 ! Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)16-Feb-14 7:14
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)16-Feb-14 7:14 
GeneralMy vote is 5 Pin
joginder-banger13-Feb-14 8:58
professionaljoginder-banger13-Feb-14 8:58 
GeneralRe: My vote is 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)13-Feb-14 17:30
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)13-Feb-14 17:30 
GeneralMy vote of 5 Pin
Carsten V2.012-Feb-14 21:53
Carsten V2.012-Feb-14 21:53 
GeneralRe: My vote of 5 Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)12-Feb-14 23:35
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)12-Feb-14 23:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.