...

View Full Version : Form Completeness Validation



jlimited
09-01-2006, 03:25 AM
Hello,

I am building a online survey for someone and I need some help. I have a php script that pulls 5 questions and the corresponding answer options from a database. Each question is displayed and a <select><option> list is generated for the answer choices, using php.

The default value of each selection box is 0 with the text "Select One".

I would like the Next button to be disabled until all 5 questions have been answered.

Incomplete surveys are not valid for the research project.

Thanks
Jlimited

probey20
09-01-2006, 04:00 PM
Give this a try - it works in IE but has not been tested in other browsers.


<html>
<head>
<title>Test Page</title>
<script language="JavaScript">
<!--
function validateSelectBox()
{
var objForm = document.frmSurvey;

with (objForm)
{
var selectBoxes = document.getElementsByTagName("SELECT");
var valid = true;

for (var j = 0; j < selectBoxes.length; j++)
{
if (selectBoxes[j].selectedIndex == 0)
{
valid = false;
break;
}
}
if (valid)
document.getElementById("nextButton").disabled = false;
else
document.getElementById("nextButton").disabled = true;
}
}
-->
</script>
</head>

<body onLoad="javascript:validateSelectBox();">

<form name="frmSurvey" method="post" action="resultPage.html">
<select name="Q1" onChange="javascript:validateSelectBox();">
<option value="0">Select One</option>
<option value="1">Option 1</option>
</select>
<select name="Q2" onChange="javascript:validateSelectBox();">
<option value="0">Select One</option>
<option value="1">Option 1</option>
</select>
<select name="Q3" onChange="javascript:validateSelectBox();">
<option value="0">Select One</option>
<option value="1">Option 1</option>
</select>
<select name="Q4" onChange="javascript:validateSelectBox();">
<option value="0">Select One</option>
<option value="1">Option 1</option>
</select>
<select name="Q5" onChange="javascript:validateSelectBox();">
<option value="0">Select One</option>
<option value="1">Option 1</option>
</select>
<input type="submit" id="nextButton" value="Next"/>
</form>

</body>
</html>

jlimited
09-01-2006, 11:25 PM
Thanks that works great



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum