Validating multiple checkbokes using Javascript
Moderator: General Moderators
-
dream2rule
- Forum Contributor
- Posts: 109
- Joined: Wed Jun 13, 2007 5:07 am
Validating multiple checkbokes using Javascript
Hello All,
I would like to know how do i validate an array of checkboxes? the form should not be submitted/posted unless atleast 1 checkbox is checked. I have around 25 checkboxes and am displaying them using array.
Regards,
Dream2rule
I would like to know how do i validate an array of checkboxes? the form should not be submitted/posted unless atleast 1 checkbox is checked. I have around 25 checkboxes and am displaying them using array.
Regards,
Dream2rule
-
dream2rule
- Forum Contributor
- Posts: 109
- Joined: Wed Jun 13, 2007 5:07 am
feyd | Please use
Where priviliges[] is taken as an array of length 25
feyd | Please use[/syntax]
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
i tried looping it but in vain..
Here's the snippet of code:
[syntax="javascript"]
var i;
for(i=0; i < document.form1.priviliges.length; i++)
{
document.write("document.form1.priviliges[i].length");
if(document.form1.priviliges[i].checked == false)
{
alert("You must select at least one entry.");
return false;
}
}
return true;
feyd | Please use[/syntax]
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]-
dream2rule
- Forum Contributor
- Posts: 109
- Joined: Wed Jun 13, 2007 5:07 am
-
dream2rule
- Forum Contributor
- Posts: 109
- Joined: Wed Jun 13, 2007 5:07 am
Code: Select all
<tr>
<td height="36" valign="top"><span class="style1"><strong>Privileges:</strong></span></td>
<td colspan="2">Code: Select all
<?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>";
?>Code: Select all
</td>
</tr>This is wrong:
It's your homework to find the mistake 
You have to use string concatenation or put {} around array variables.
Code: Select all
$count = count($priviliges)."<br>";
//echo $count;
for($i = 0; $i < $count; $i++ )Code: Select all
echo "<td align=left><input type=checkbox name=priviliges[] value='$priviliges[$i]'>$priviliges[$i]</td>";There are 10 types of people in this world, those who understand binary and those who don't
You have wrong validation too. In fact, your validation checks whether ALL checkboxes have been checked. Should be something like this:dream2rule wrote:Code: Select all
var i; for(i=0; i < document.form1.priviliges.length; i++) { document.write("document.form1.priviliges[i].length"); if(document.form1.priviliges[i].checked == false) { alert("You must select at least one entry."); return false; } } return true;
Code: Select all
var i;
for(i=0; i < document.form1.priviliges.length; i++)
{
if(document.form1.priviliges[i].checked)
{
return true;
}
}
alert("You must select at least one entry.");
return false;
There are 10 types of people in this world, those who understand binary and those who don't