Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-19-2005, 10:42 PM   PM User | #1
m75131
New to the CF scene

 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
m75131 is an unknown quantity at this point
PHP Array[] Javascript Validation Help Needed

Hi,

I have a .php page that have a multiple check box question that requires validation whether or not the user checked any values.

-----------------------------
HTML:
-----------------------------
<p><font color="#FF0000">*</font> If you are a health professional, what is your practice setting (check all that apply):<br>
<input name="Q2_PracticeSetting[]" type="checkbox" value="Hospital">
Hospital<br>
<input name="Q2_PracticeSetting[]" type="checkbox" value="Outpatient setting">
Outpatient setting<br>
<input name="Q2_PracticeSetting[]" type="checkbox" value="Academia">
Academia<br>
<input name="Q2_PracticeSetting[]" type="checkbox" value="Research">
Research<br>
<input name="Q2_PracticeSetting[]" type="checkbox" value="Other">
Other
</p>

-----------------------------
Using Javascript:
-----------------------------
function ValidateForm() {

field = document.register.firstname;
if (isBlank(field, "First Name")) return false;

field = document.register.lastname;
if (isBlank(field, "Last Name")) return false;

field = document.register.credential;
if (isBlank(field, "Credentials")) return false;

field = document.register.email;
if (isBlank(field, "Email Address")) return false;
if (!isEmail(field, "Email Address")) return false;

var chks = document.register.elements['Q2_PracticeSetting[]'];
var hasChecked = true;
for (var i=0;i<chks.length;i++){
if (chks[i].checked){
hasChecked = false;
break;
}
}
if (!hasChecked){
alert("Please select at least one.");
chks[0].focus();
return false;
}


return true;
}

-----------------------------
Result:
-----------------------------
Not working

Any help is much apprciated!
m75131 is offline   Reply With Quote
Old 04-19-2005, 11:45 PM   PM User | #2
hemebond
Senior Coder

 
Join Date: Jul 2004
Location: New Zealand
Posts: 1,315
Thanks: 0
Thanked 2 Times in 2 Posts
hemebond is an unknown quantity at this point
It looks like you've got it backwards
Code:
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
	if (chks[i].checked)
	{
		hasChecked = true;
		break;
	}
}
Try that.
__________________
Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
I code to specs, and test only in Firefox (unless stated otherwise).
hemebond is offline   Reply With Quote
Old 04-20-2005, 01:31 AM   PM User | #3
sonjay
New to the CF scene

 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
sonjay is an unknown quantity at this point
JavaScript doesn't like the [] as part of the input name. But of course you need that for your php array.....

Solution? Add id="Q2_PracticeSetting" to the checkbox fields, and javascript will happily use that, while php will continue to pull the array name properly.
sonjay is offline   Reply With Quote
Old 04-20-2005, 02:14 AM   PM User | #4
hemebond
Senior Coder

 
Join Date: Jul 2004
Location: New Zealand
Posts: 1,315
Thanks: 0
Thanked 2 Times in 2 Posts
hemebond is an unknown quantity at this point
Quote:
Originally Posted by sonjay
Add id="Q2_PracticeSetting" to the checkbox fields, and javascript will happily use that, while php will continue to pull the array name properly.
id values must be unique.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
	<head>
		<title>57321</title>
		<meta name="description" content="Validate multiple input elements">
	</head>
	<body>
		<form action="57321" method="get" onsubmit="return validateForm()">
			<fieldset>
				<legend>If you are a health professional, what is your practice setting (check all that apply)</legend>
				<div>
					<input id="hospital" name="Q2_PracticeSetting[]" type="checkbox" value="Hospital">
					<label for="hospital">Hospital</label>
				</div>
				<div>
					<input id="outpatient" name="Q2_PracticeSetting[]" type="checkbox" value="Outpatient setting">
					<label for="outpatient">Outpatient setting</label>
				</div>
				<div>
					<input id="academia" name="Q2_PracticeSetting[]" type="checkbox" value="Academia">
					<label for="academia">Academia</label>
				</div>
				<div>
					<input id="research" name="Q2_PracticeSetting[]" type="checkbox" value="Research">
					<label for="research">Research</label>
				</div>
				<div>
					<input id="other" name="Q2_PracticeSetting[]" type="checkbox" value="Other">
					<label for="other">Other</label>
				</div>
				<input type="submit">
			</fieldset>
		</form>

		<script type="text/javascript">
			function validateForm()
			{
				var chks = document.getElementsByName('Q2_PracticeSetting[]');
				var hasChecked = false;
				for (var i = 0; i < chks.length; i++)
				{
					if (chks[i].checked)
					{
						hasChecked = true;
						break;
					}
				}
				if (!hasChecked)
				{
					alert("Please select at least one.");
					chks[0].focus();
					return false;
				}
				return true;
			}
		</script>
	</body>
</html>
__________________
Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
I code to specs, and test only in Firefox (unless stated otherwise).

Last edited by hemebond; 04-20-2005 at 02:24 AM..
hemebond is offline   Reply With Quote
Old 09-19-2009, 03:53 PM   PM User | #5
isaacqc
New to the CF scene

 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
isaacqc is an unknown quantity at this point
thanks to sonjay and hemebond!!! it helps!!
isaacqc is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:20 PM.


Advertisement
Log in to turn off these ads.