Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
show a javascript alert if no checkboxlist was checked in a form, you need to validate the checked property of that checkboxlist. By using the onSubmit event of the form and a JS function .can validate form correctly.
Posted

Sample HTML

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()" />




Sample Javascript


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>
 
Share this answer
 
Comments
Member 10915676 2-Jul-14 3:07am    
thanks. this is nice working
C#
function chkselect() {
    var cnt = 0;
    for (var i = 50; i < document.CtrlForm.elements.length; i++) {

        if (document.CtrlForm.elements[i].type == 'checkbox') {

            if (document.CtrlForm.elements[i].checked == true) {
                cnt++;
            }
        }
    }
    if (cnt == 0) {
        document.getElementById('lblError').innerHTML = 'Atleast 1 Request Should be Selected..';
        return false;
    }
}
 
Share this answer
 
[]
 
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