PDA

View Full Version : Multiple Radio Button JavaScript Validation



Be Loud Online
21 Feb 2012, 01:55 PM
I have created a questionnaire using PHP that totals the score of your answers. 1 point for yes, 0 points for no. If you score between 0 and 10 you are brought to landing page A, between 11 and 20 go to landing page B, etc, etc.

Here is some of the code so you can see what I mean:



<form name="test" action="SelfTest.php" onsubmit="return validateForm()" method="post">
<ol>
<li>Do you brush your teeth daily with fluoridated toothpaste?<br />
<input type="radio" value="1" name="no1">Yes &nbsp;&nbsp;<input type="radio" value="0" name="no1">No
</li><br />


What I am having trouble with is validating the answers. The result I am looking for is if a question is unanswered, the visitor will be prompted to go back and answer that particular question. "Question 4 was not answered. Please complete before submitting your results." kinda thing.

The code I am using is not working and I can not figure out why. I am also very sure that there is a shorter and cleaner way of doing this. Any help is GREATLY appreciated...

Here is the nonworking JavaScript shortened to the first 2 questions (there are 50 questions total):



<script type="text/javascript">
function validateForm()
{
if (document.forms["test"]["no1"].value==null || document.forms["test"]["no1"].value=="")
{
alert("Question 1 was not answered. Please complete before submitting.");
return false;
}
else if (document.forms["test"]["no2"].value==null || document.forms["test"]["no2"].value=="")
{
alert("Question 2 was not answered. Please complete before submitting.");
return false;
}
}
</script>