hi all,
i am having some check boxes in my page. so i dont know the number because it is cuming from data base, so i need a javascript validation for this check boxes because he should check atleast one check box. so can any one please...
here is my 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>

Recommended Answers

All 4 Replies

This is one js function , which will help u .

function continue()
{
	 var hasSelected = false;
	 var FormObj = document.formName; // formName => name of form tag
	 
	 var chklen = FormObj.elements.length;
	 for( var n = 0; n < chklen; n++ )
	 {
		currobj = FormObj.elements[n];
		if(currobj.type == "checkbox" && currobj.checked)
			hasSelected = true;
	}
	if( hasSelected )
	{
		alert("One checkbox is checked.");
		return true;
	}
	else
	{  
	   alert( "Please check at least one Record." );
	   return false;
	}
}

Hey,

You can call the above function by @vibhadevit in your of submit button onclick="continue()" or in form as onsubmit="continue()".

Member Avatar for rajarajan2017

Sample:

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script language="javascript">
	function testing(){
		 var hasSelected = false;
		 var FormObj = document.formName; // formName => name of form tag
		 
		 var chklen = FormObj.elements.length;
		 for( var n = 0; n < chklen; n++ )
		 {
			currobj = FormObj.elements[n];
			if(currobj.type == "checkbox" && currobj.checked)
				hasSelected = true;
		}
		if( hasSelected )
		{
			alert("One or more checkboxes are checked.");
			return true;
		}
		else
		{  
		   alert( "Please check at least one Record." );
		   return false;
		}
	}
</script>
</head>
<body>
<form name="formName" method="post" action="">
<b>Your Favorite Scripts & Languages</b><br>
<input type="checkbox" name="list" value="Java">Java<br>
<input type="checkbox" name="list" value="Javascript">Javascript<br>
<input type="checkbox" name="list" value="Active Server Pages">Active Server Pages<br>
<input type="checkbox" name="list" value="HTML">HTML<br>
<input type="checkbox" name="list" value="SQL">SQL<br>
<input type="submit" name="commit" value="Submit" onClick="testing()"/>
</form>
</body>
</html>

ya i got it. Thank u...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.