validate multiple checkboxes using javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dream2rule
    New Member
    • Jul 2007
    • 13

    validate multiple checkboxes using javascript

    Hello All,

    I am trying to validate multiple checkboxes whose values are stored in an array using php. I have been trying from a really long time but nothing's working out.

    Can anyone help?

    Here's the code:

    Code:
    <form id="create_user" name="create_user" method="post" action="creating_database.php" onsubmit="return validate_create_user_form();">
    <?php
    $priviliges = array("SELECT","INSERT","UPDATE","DELETE","FILE","CREATE","ALTER","INDEX","DROP","CREATE TEMPORARY TABLES","CREATE VIEW","SHOW VIEW","CREATE ROUTINE","ALTER ROUTINE","EXECUTE","GRANT","SUPER","PROCESS",
    "RELOAD","SHUTDOWN","SHOW DATABASES","LOCK TABLES", "REFERENCES", "REPLICATION CLIENT", "REPLICATION SLAVE");
    							
    echo "<table border=0 cellpadding=0 cellspacing=0 align=center width='100%' border='1'>";
    $count = count($priviliges)."<br>";
    //echo $count;
    
    for($i = 0; $i < $count; $i++ ) 
    {
    	if($i == 0)
    		echo "<tr>";
    			
    	echo "<td align=left><input type=checkbox name=priviliges[] 
    value='$priviliges[$i]'>$priviliges[$i]</td>";
    	echo "</tr>";
    }
    
    echo "</table>";
    ?>
    </form>
    I would want to validate the priviliges[] array which consists of the above mentioned values. If no privilige is checked, i want to display an error message saying to check atleast one privilige.

    Any help would be appreciated.

    Thanks and Regards,
    Dream2rule
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use [CODE=javascript]document.getEle mentsByName("pr iviliges[]");[/CODE] to get all the checkboxes in an array. Then loop over the checkboxes and keep checking the checked property. If none are checked, alert the error message. I assume you would do this onsubmit.

    PS. privilege is spelled incorrectly!

    Comment

    • dream2rule
      New Member
      • Jul 2007
      • 13

      #3
      Tried doing it this way but it does not help. :(

      Code:
      for ( var i=0; i < document.getElementsByName("priviliges[]"); i++) 
      	//priviliges is the name of the group of checkboxes
      	{
      			if (document.getElementsByName("priviliges[i]").checked) 
      			{
      				var priviliges_checked[i] = document.getElementsByName("priviliges[i]").value;
      			}
      	}
      	if( (priviliges_checked[i] == null) || (priviliges_checked[i] == "") )
      	{
      		alert("Please check atleast 1 privilege!");
      		return false;
      	}

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        [CODE=javascript]var privs = document.getEle mentsByName("pr iviliges[]");[/CODE] will give you an array. To get the count, use:
        [CODE=javascript]privs.length[/CODE] and to index an item in the array:
        [CODE=javascript]privs[i][/CODE] so your code could be:
        [CODE=javascript]var privs = document.getEle mentsByName("pr iviliges[]");
        var privschecked = false;
        for (var i = 0; i < privs.length; i++) {
        if (privs[i].checked) {
        privschecked = true;
        break;
        }
        }
        if (!privschecked) {
        alert('blah');
        return false;
        }
        return true;[/CODE]

        Comment

        • dream2rule
          New Member
          • Jul 2007
          • 13

          #5
          thanks a lot.. it helped :)

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Glad it helped.

            Comment

            • Irulappan P
              New Member
              • Nov 2011
              • 1

              #7
              Hi,
              your code is very useful for me .thanks alot...

              Comment

              Working...