...

View Full Version : PHP Array[] Javascript Validation Help Needed



m75131
04-19-2005, 10:42 PM
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!

hemebond
04-19-2005, 11:45 PM
It looks like you've got it backwards
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}Try that.

sonjay
04-20-2005, 01:31 AM
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.

hemebond
04-20-2005, 02:14 AM
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.
<!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>

isaacqc
09-19-2009, 03:53 PM
thanks to sonjay and hemebond!!! it helps!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum