Validate ASP.Net DropDownList CheckBoxList Server control using JavaScript

Last Reply on Apr 30, 2015 01:26 AM By NarenderC

Posted on Apr 29, 2015 08:05 AM

Hi,

How to validate DropDownCheckBoxes control at client side(javascript or Jquery)? I would like to restrict the user to submit the page if no item is selected in the dropdowncheckboxes control. i.e no check box is checked.

Please provide the Javascript/Jquery code to validate the control at client side.

 

Thanks and regards,

Narender Charla

Posted on Apr 29, 2015 09:03 PM

Refer this code

Javascript

  <script type="text/javascript">
        function Validate() {
            var ddl = document.getElementById('<%=DropDownCheckBox.ClientID%>'); // Replace DropDownCheckBox with your Control Id
            if (ddl.selectedIndex == -1) {
                alert('Please Select a Value');
            }
            return false;
        }
    </script>

HTML

<asp:Button ID="Button1" runat="server" Text="Validate" OnClientClick="return Validate()" />

 


Posted on Apr 30, 2015 01:26 AM

Hi Shaiwal,

Thanks for your reply. I am able to validate dropdown with checkboxes control by following your code as reference. Below is the code which i used. 

<script type="text/javascript">
     function Validate() {
         var ddl = document.getElementById('<%=DropDownCheckBox.ClientID%>'); // Replace DropDownCheckBox with your Control Id
         var checkedCount = 0;
         for (var i = 0; i < ddl.children.length; i++)
          {
              if (ddl.children[i].type == 'checkbox') {
                  if (ddl.children[i].checked)
                  {
                      checkedCount = checkedCount  + 1;
                      break;
                  }    
              }             
         }

          if (checkedCount > 0) {
             return true;
         }
         else {
             alert('Please Select atleast one checkbox');
             return false;
         }        

     }
</script>

 

<asp:Button ID="Button1" runat="server" Text="Validate" OnClientClick="return Validate()" />

Thanks a lot 

 

-Narender