Check box validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • niths
    New Member
    • Mar 2010
    • 18

    Check box validation

    hi all,
    i am having a page where there are some checkboxes in it. i cant give exact number because i am getting that from database. so now if the user didnt check atleast one check box i need to get a popup. so can any one help me please..its urgent..
    Thank u..
    Code:
    <table border="1" cellspacing="0" cellpadding="0" align="center" style="width: 500px" bordercolor="red">
        <tr>
            <th></th>
            <th style="color: navy;">Userid</th>
            <th style="color: navy;">Username</th>
               
        </tr>
        <?php
        $sno=1;
        while($row=mysql_fetch_array($result2, MYSQL_ASSOC)){
        ?>
         <tr>
    <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['userid']; ?>"></td>
    <td><? echo $row['userid']; ?></td>
    <td><? echo $row['username'];?></td>
    </tr>
    <?php
        $sno=$sno+1;
    }
    ?>
        </table>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    a popup is a client-side action, thus PHP can’t be (directly) involved.

    to check whether no checkbox was selected, loop over all checkboxes and test for the .checked property.

    Code:
    // returns true if at least one box is checked, otherwise false
    function checkBox()
    {
    	var i, list = document.forms[0]["checkbox[]"];
    	for (i = list.length; i--;) {
    		if (list[i].checked) {
    			return true;
    		}
    	}
    	return false;
    }

    Comment

    Working...