Page 1 of 1
Validating multiple checkbokes using Javascript
Posted: Tue Aug 21, 2007 11:38 pm
by dream2rule
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
Posted: Tue Aug 21, 2007 11:40 pm
by feyd
Iterate over them checking each one until one is found to be checked.
That's about as specific as we can get without making assumptions about your code.
Posted: Tue Aug 21, 2007 11:46 pm
by dream2rule
feyd | Please use 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;
Where priviliges[] is taken as an array of length 25
feyd | Please use[/syntax]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]
Posted: Tue Aug 21, 2007 11:56 pm
by feyd
I'll take a stab and say "document.form1.priviliges" doesn't exist.
Try document.forms['form1'].elements['privilgies[]'] instead. Just so you know, it's spelled "privileges."
Posted: Wed Aug 22, 2007 12:25 am
by dream2rule
that doesn't work even.
Posted: Wed Aug 22, 2007 12:52 am
by feyd
You're to have to post your HTML.
Posted: Wed Aug 22, 2007 4:19 am
by dream2rule
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>";
?>
Posted: Wed Aug 22, 2007 6:39 am
by VladSun
This is wrong:
Code: Select all
$count = count($priviliges)."<br>";
//echo $count;
for($i = 0; $i < $count; $i++ )
It's your homework to find the mistake
Code: Select all
echo "<td align=left><input type=checkbox name=priviliges[] value='$priviliges[$i]'>$priviliges[$i]</td>";
You have to use string concatenation or put {} around array variables.
Posted: Wed Aug 22, 2007 6:46 am
by VladSun
dream2rule wrote:that doesn't work even.
Please, describe.
Posted: Wed Aug 22, 2007 6:49 am
by VladSun
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;
You have wrong validation too. In fact, your validation checks whether ALL checkboxes have been checked. Should be something like this:
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;
Untested!