Multiple check box validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shanmugamit
    New Member
    • Feb 2008
    • 45

    Multiple check box validation

    hi,
    Code:
    var len=document.form.check.length
    flag=false;
    for(i=0;i<len;i++)
    {
    if(document.form.check[i].checked==true)
    {
    flag=true;
    break;
    }
    }
    if(flag==false)
    {
    alert(select atlest one)
    }
    my error is object is required
  • shanmugamit
    New Member
    • Feb 2008
    • 45

    #2
    Originally posted by shanmugamit
    hi,my error is object is required.please any one help for a problem.....
    Code:
    var len=document.form.check.length
    flag=false;
    for(i=0;i<len;i++)
    {
    if(document.form.check[i].checked==true)
    {
    flag=true;
    break;
    }
    }
    if(flag==false)
    {
    alert(select atlest one)
    }

    Comment

    • vee10
      New Member
      • Oct 2006
      • 141

      #3
      Hi ,

      once check whether "form" is the id of the form and check is the id of the checkbox because i am not getting error and this code is fine


      Originally posted by shanmugamit
      Code:
      var len=document.form.check.length
      flag=false;
      for(i=0;i<len;i++)
      {
      if(document.form.check[i].checked==true)
      {
      flag=true;
      break;
      }
      }
      if(flag==false)
      {
      alert(select atlest one)
      }

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by shanmugamit
        Code:
        alert(select atlest one)
        alert('select at least one') - it should be a string.

        Comment

        • shanmugamit
          New Member
          • Feb 2008
          • 45

          #5
          Mutiple check box validation

          i have mutiple check box value error object required.......
          Code:
          aa
          <script type="text/javascript">
          function fnallocate()
          {
          var len=document.form.selcie.length
          flag=false;
          for(i=0;i<len;i++)
          {
          if(document.form.selcie[i].checked==true)
          {
          flag=true;
          break;
          }
          }
          if(flag==false)
          {
          alert("select atlest one");
          }
          </script>
          <body>
          <form action="checkbox.php" name="form" method="post">
          <?
           $res=mysql_query("select * from cie where des='Resource Pipeline Associate' order by name");
          $j=0;
          		$norow=mysql_num_rows($res);
          		$flag=false;
          	while($td=mysql_fetch_array($res))
          	{
          				for($i=0;$i<count($can);$i++)
          
          	        {	
          					if("$td[1]"=="$can[$i]")
          					{
          						$flag=true;
          						$i=count($can);
          					}
          					else
          					{
          					$flag=false;
          					}
          			}
          				if($flag==true)
          				{
          //print"		<tr><td><input type=\"checkbox\" name=\"selcie\"  value=\"$td[1]\" checked>$td[1]</td></tr>";
          				}
          				else
          				{
          print"		<tr><td><input type=\"checkbox\" name=\"selcie\" value=\"$td[1]\" >$td[1]</td></tr>";				
          				}
          				$j++;
          	}	
          ?>		 
            <input type="submit" name="Submit" value="Submit" onclick="fnallocate()">
          </form>
          <?
           
           
          if(isset($_POST['Submit']))
          {
              for ($i=0; $i<count($_POST['selcie']);$i++) {
                 echo "<br />value $i = ".$_POST['selcie'][$i];
              }
          }
          ?>
          </body

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            [php]
            <script type="text/javascript">
            function fnallocate(len) {
            for(i=0;i<len;i ++) {
            if(document.get elementById('se lcie_'+i).check ed==true)
            return true;
            }
            alert("select atlest one");
            return false;
            }
            </script>
            <body>
            <form action="checkbo x.php" name="form" method="post">
            <?
            $res=mysql_quer y("select * from cie where des='Resource Pipeline Associate' order by name");
            $j=0;
            $norow=mysql_nu m_rows($res);
            $flag=false;
            while($td=mysql _fetch_array($r es))
            {
            for($i=0;$i<cou nt($can);$i++)
            {
            if($td[1]==$can[$i])
            {
            $flag=true;
            $i=count($can);
            }
            else
            {
            $flag=false;
            }
            }
            if($flag==true)
            {
            echo "<tr><td><i nput type=\"checkbox \" id=\"selice_".$ j."\" name=\"selcie[]\" value=\"$td[1]\" checked>$td[1]</td></tr>";
            }
            else
            {
            echo "<tr><td><i nput type=\"checkbox \" id=\"selice_".$ j."\" name=\"selcie[]\" value=\"$td[1]\" >$td[1]</td></tr>";
            }
            $j++;
            }
            ?>
            <input type="submit" name="Submit" value="Submit" onclick="return fnallocate(".($ j-1).")">
            </form>
            <?
            if(isset($_POST['Submit']))
            {
            for ($i=0; $i<count($_POST['selcie']);$i++)
            {
            echo "<br />value $i = ".$_POST['selcie'][$i];
            }
            }
            ?>
            </body>
            [/php]

            Try it this time...
            Last edited by hsriat; Feb 26 '08, 07:45 AM. Reason: It was checkbox, not select

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by shanmugamit
              i have mutiple check box value error object required...
              Threads merged. Please do not double post.

              Moderator

              Comment

              Working...