Validating multiple checkbokes using Javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Validating multiple checkbokes using Javascript

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

feyd | Please use

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;

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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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."
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

that doesn't work even.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're to have to post your HTML.
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post 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>";
		?>

Code: Select all

</td>
   	</tr>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

dream2rule wrote:that doesn't work even.
Please, describe.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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!
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply