Checkboxlist Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sheenattt
    New Member
    • Nov 2006
    • 20

    Checkboxlist Validation

    I am using checkboxlist in asp.net.I cant get an alert with JavaScripting when the checkboxlist is not selected.For checkbox the command is checkbox.checke d,can anyone tell me what is the command for checkboxlist?
  • vssp
    Contributor
    • Jul 2006
    • 268

    #2
    <!-- This script is available free online at MediaZeal Web Design --->
    <!-- http://mediazeal.com --->


    <script language="JavaS cript" type="text/JavaScript">
    <!--//hide script
    function checkme() {
    missinginfo = "";
    if (!document.form .agree.checked) {
    missinginfo += "\n - You must agree to the terms";
    }
    if (missinginfo != "") {
    missinginfo ="_____________ _______________ ______\n" +
    "Required information is missing: \n" +
    missinginfo + "\n____________ _______________ _______" +
    "\nPlease complete and resubmit.";
    alert(missingin fo);
    return false;
    }
    else {
    return true;
    }
    }

    // --->
    </script>



    Paste this code into the body section
    <form name="form" method="post" action="#" onSubmit="retur n checkme();">
    <table cellpadding="0" cellspacing="0" border="0">
    <tr>
    <td>Name: </td>
    <td> <input type="text" size="20" name="name"></td>
    </tr>
    <tr>
    <td>Email: </td>
    <td> <input type="text" size="20" name="email"></td>
    </tr>
    <tr>
    <td colspan="2" align="center"> <input type="checkbox" name="agree" value="agree_te rms"> I agree to the terms</td>
    </tr>
    <tr>
    <td colspan="2" align="center"> <input type="submit" name="submit" value="Submit"> <input type="reset" name="reset" value="Clear"></td>
    </tr>
    </table>
    </form>

    <!--

    This script is available free online at MediaZeal Web Design --->

    Comment

    • vee10
      New Member
      • Oct 2006
      • 141

      #3
      hi,

      I think this solves ur problem

      Code:
      <html xmlns="http://www.w3.org/1999/xhtml" >
      <head runat="server">
          <title>Untitled Page</title>
          <script type="text/javascript">
       function  fuc()
       {
       
      var chkList1= document.getElementById ("names");
      var arrayOfCheckBoxes= chkList1.getElementsByTagName("input");
      for(var i=0;i<arrayOfCheckBoxes.length;i++)
      {
      alert(arrayOfCheckBoxes[i].checked);
      }
      }
        
       
       </script>
       
      </head>
      <body>
          <form id="form1" runat="server">
       
       <asp:CheckBoxList ID="names" runat="server">
       
           <asp:ListItem Value="1" Text="1"  ></asp:ListItem>
           <asp:ListItem Value="2" Text="2" ></asp:ListItem>
           <asp:ListItem Value="3" Text="3" ></asp:ListItem>
           <asp:ListItem Value="4" Text="4" ></asp:ListItem>
           <asp:ListItem Value="5" Text="5" ></asp:ListItem>
           
           </asp:CheckBoxList>
      
          
        
          </form>
      </body>
      </html>















      Originally posted by sheenattt
      I am using checkboxlist in asp.net.I cant get an alert with JavaScripting when the checkboxlist is not selected.For checkbox the command is checkbox.checke d,can anyone tell me what is the command for checkboxlist?

      Comment

      • sheenattt
        New Member
        • Nov 2006
        • 20

        #4
        Thank you for your help but this is not working for Checkboxlist.If you can help plz tell me what shld I use for checkboxlist.

        Comment

        • vee10
          New Member
          • Oct 2006
          • 141

          #5
          hi,

          once post ur code so that i can understand because the one i posted is for the checkboxlist only










          Originally posted by sheenattt
          Thank you for your help but this is not working for Checkboxlist.If you can help plz tell me what shld I use for checkboxlist.

          Comment

          • sheenattt
            New Member
            • Nov 2006
            • 20

            #6
            Thanku so much for your help here am pasting my code,do help me.Thanx

            Code:
            void Page_Load()
                {
                    Button1.Attributes.Add("onClick", "return checkbox_checker()");
                }
            
                protected void Button1_Click1(object sender, EventArgs e)
                {
                    //further action
                }
            </script>
            <html>
            <head>
            <SCRIPT LANGUAGE="JavaScript">
            
            function checkbox_checker()
            {
            for (counter = 0; counter < checkbox_form.checkbox.length; counter++)
            {
            if (checkbox_form.checkbox[counter].checked)
            {
             return true;
            }
            else
            {
            alert("Not checked");
            return false;
            }
            }
            
            }
            </script>
            
            
            </head>
            <body>
                <form id=checkbox_form runat="server">
                    <p style="text-align: left">  
                        <asp:CheckBoxList ID="checkbox1" runat="server">
                            <asp:ListItem>sheena</asp:ListItem>
                            <asp:ListItem>sheenat</asp:ListItem>
                            <asp:ListItem>sheenatt</asp:ListItem>                
                        </asp:CheckBoxList>&nbsp;</p>
                    <p style="text-align: left">
                        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
                    </p>      
                   
                </form>
            </body>
            </html>

            Comment

            • vee10
              New Member
              • Oct 2006
              • 141

              #7
              hi,
              write like this
              <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick=" checkbox_checke r()" />

              place this script



              <script type="text/javascript">

              function checkbox_checke r1()
              {

              var flag=0;
              var chkList1= document.getEle mentById ("checkbox1" );
              var arrayOfCheckBox es= chkList1.getEle mentsByTagName( "input");
              for(counter = 0; counter<arrayOf CheckBoxes.leng th; counter++)
              {
              if (arrayOfCheckBo xes[counter].checked)
              {
              flag=1;
              }
              else
              {
              alert("not checked");
              return false;
              }
              }
              if(flag==1)
              {
              alert('checked' );
              return true;
              }
              }
              </script>




              Originally posted by sheenattt
              Thanku so much for your help here am pasting my code,do help me.Thanx

              Code:
              void Page_Load()
                  {
                      Button1.Attributes.Add("onClick", "return checkbox_checker()");
                  }
              
                  protected void Button1_Click1(object sender, EventArgs e)
                  {
                      //further action
                  }
              </script>
              <html>
              <head>
              <SCRIPT LANGUAGE="JavaScript">
              
              function checkbox_checker()
              {
              for (counter = 0; counter < checkbox_form.checkbox.length; counter++)
              {
              if (checkbox_form.checkbox[counter].checked)
              {
               return true;
              }
              else
              {
              alert("Not checked");
              return false;
              }
              }
              
              }
              </script>
              
              
              </head>
              <body>
                  <form id=checkbox_form runat="server">
                      <p style="text-align: left">  
                          <asp:CheckBoxList ID="checkbox1" runat="server">
                              <asp:ListItem>sheena</asp:ListItem>
                              <asp:ListItem>sheenat</asp:ListItem>
                              <asp:ListItem>sheenatt</asp:ListItem>                
                          </asp:CheckBoxList>&nbsp;</p>
                      <p style="text-align: left">
                          <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
                      </p>      
                     
                  </form>
              </body>
              </html>

              Comment

              • anil4scripts
                New Member
                • May 2007
                • 1

                #8
                Thanks buddies..

                its been so useful to us....

                Happy programming.. :)

                Comment

                Working...