ASP.Net CheckBoxList client side JavaScript validation

If you need to validate CheckBoxList on client side using JavaScript, here is a way to do it. This code will check that at least one checkbox is selected, return true if it is and false if there are no selections.

function Validate() {
     //Other control validation goes here...
     
     var buSelected = false;
     var relevantBUs = document.getElementById("cblBusinessUnits");

     var businessUnit = relevantBUs.getElementsByTagName("input");
     for (var i = 0; i < businessUnit.length; i++) {
          if (businessUnit[i].checked) {
               buSelected = true;
               return;
          }
     }

     if (!buSelected) {
          alert("Please select at least one option.");
          return false;
     }
            
     return true;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s