Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

How to validate a checkbox List in asp.net.


Thanks
Mohd. Wasif
Posted

you need to use javascript, write a function in javascript as below, it will validate checkbox list
JavaScript
<script language="javascript" type="text/javascript"> 
function ValidateASPControls() 
    { 
        var oItems;

        oItems=document.getElementById("oItems"); 
        var chkList = oItems.getElementsByTagName("input"); 
        chkFlag=0; 
        
        for (var i = 0; i < chkList.length; i++) 
        { 
            if(chkList[i].checked) 
            { 
             chkFlag=1; 
            } 
        } 
         
        if (chkFlag==0) 
        { 
            alert("Please select an item") 
            return false; 
        } 
         
   } 
</script>
 
Share this answer
 
Comments
Mohd Wasif 29-Aug-11 5:39am    
Please Clarify
what is oItems and input.

If I am not worng then oItems is CheckBoxList control Id
and what about input
koolprasad2003 29-Aug-11 7:35am    
YES, "oItems" is checkbox control "input" are the no of checkboxes.
You can't use any validation control on the check box list,
only you can do the validation using the
VB
JavaScript or in code behind u have to write your own validation logic
 
Share this answer
 
XML
function ValidateModuleList(source, args) {
              var chklist = document.getElementById('<%= chklist.ClientID %>');
              var chkListinputs = chklist.getElementsByTagName("input");
              for (var i = 0; i < chkListinputs.length; i++) {
                  if (chkListinputs[i].checked) {
                      args.IsValid = true;
                      return;
                  }
              }
              args.IsValid = false;
          }


<asp:CheckBoxList ID="chklist" runat="server"></asp:CheckBoxList>
    <asp:CustomValidator  runat="server" ID="cvmodulelist" ForeColor="Red"
 ClientValidationFunction="ValidateModuleList"
 ErrorMessage="Please select atleast one service scenario" ></asp:CustomValidator>
 
Share this answer
 
Please explain you question in details so that other one can help you


C#
<script>
       function ValidateCheckBoxList() {

           var listItems = document.getElementById("CheckBoxList1").getElementsByTagName("input");
           var itemcount = listItems.length;
           var iCount = 0;
           var isItemSelected = false;
           for (iCount = 0; iCount < itemcount; iCount++)
           {
               if (listItems[iCount].checked)
               {
                   isItemSelected = true;
                   break;
               }
           }
           if (!isItemSelected) {
               alert("Please select an Item.");
           }
           else {
               return true;
           }
           return false;
       }
   </script>




XML
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
        <asp:ListItem>a</asp:ListItem>
        <asp:ListItem>b</asp:ListItem>
        <asp:ListItem>c</asp:ListItem>
        </asp:CheckBoxList>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return ValidateCheckBoxList()" />
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900