469,643 Members | 2,025 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

Validate Online Test

Hello All,

Trying to figure out how to validate a series of questions on an online test. I am thinking that VB or Javascript is the best route, but your input may make a difference. The site i am working with is using .asp.

Their are 30 multiple choice questions. Each will have have 3 or 4 checkboxes where the test taker will choose only 1 answer per question. Anybody have any ideas as to the best way to validate that each question has at least one answer checked?

This form below has the functions i am trying to obtain, but does not send the results. I ge the email, but no results.

<html>

<head>
<title>Registration Form</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

function validate() {

var theMessage = "Please complete the following: \n-----------------------------------\n";
var noErrors = theMessage

// make sure field is not blank
if (document.form1.name.value=="") {
theMessage = theMessage + "\n --> Your First Name";
}

// make sure field is not blank
if (document.form1.name1.value=="") {
theMessage = theMessage + "\n --> Your Last Name";
}


// Make sure at least 1 checkbox is checked
var multiCheckbox = false;
for (i = 0; i < document.form1.session1.length; i++) {
if (document.form1.session1[i].checked)
multiCheckbox = true; }
if (!multiCheckbox) {
theMessage = theMessage + "\n --> No answere for question 1";
}

// Make sure at least 1 checkbox is checked
var multiCheckbox = false;
for (i = 0; i < document.form1.session2.length; i++) {
if (document.form1.session2[i].checked)
multiCheckbox = true; }
if (!multiCheckbox) {
theMessage = theMessage + "\n --> No answere for question 2";
}


// If no errors, submit the form
if (theMessage == noErrors) {
return true;

} else {

// If errors were found, show alert message
alert(theMessage);
return false;
}
}
// End -->
</script>
</head>

<body>

<table border="1" width="100%">
<tr>
<td width="33%">&nbsp;</td>
<td width="33%"><form name="form1" action="cgi-bin/sendmail_mike.asp" onSubmit="return validate(this);">
<table width="300" border="0" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#cccccc">
<table width="100%" border="0" cellpadding="3" cellspacing="1">
<tr bgcolor="#FFFFFF">
<td align="center">Registration Form</td>
</tr>
<tr bgcolor="#FFFFFF">
<td>First Name:<!-- field cannont be left blank --><br>
<input type="Text" name="name" size="20"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>Last Name:<!-- field cannont be left blank --><br>
<input type="Text" name="name1" size="20">
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>Which session(s) will you attend:<!-- field cannont be left blank --><br>
<input name="session1" type="checkbox" value="morning">Morning<br>
<input name="session1" type="checkbox" value="afternoon">Afternoon<br>
<input name="session1" type="checkbox" value="afternoon">Afternoon<br>
<input name="session1" type="checkbox" value="evening">Evening
<p>Which session(s) will you attend:<!-- field cannont be left blank --><br>
<input name="session2" type="checkbox" value="morning">Morning<br>
<input name="session2" type="checkbox" value="afternoon">Afternoon<br>
<input name="session2" type="checkbox" value="afternoon">Afternoon<br>
<input name="session2" type="checkbox" value="evening">Evening</p>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td><!-- field cannont be left blank --></td>
</tr>
<tr bgcolor="#FFFFFF">
<td align="center"><input name="submit" type="submit" value="submit">
&nbsp;&nbsp;<input name="reset" type="reset" value="reset">
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>


</td>
<td width="34%">&nbsp;</td>
</tr>
</table>

</body>

</html>


Really appreciate your time and expertise....

mike
Aug 7 '07 #1
1 2633
jhardman
3,406 Expert 2GB
Mike,

Rather than use a submit button, just use a <input type="button" onClick="validate()"> and make a javascript function that checks all the fields. If the fields are left blank, the javascript function should make that input field change colors, otherwise the function should submit the form. I'm not an expert on javascript, but this is pretty easy (and I probably don't have too many errors):[html]
function validate()
{
var complete = true;
if (document.all.form1.fname.value=="")
{
document.all.form1.fname.style.color='red';
var complete = false;
}

if (document.all.form1.lname.value=="")
{
document.all.form1.lname.style.color='red';
var complete = false;
}

/etc

if (complete == true)
{ location.href="/nextPage.asp"
}
else
{
alert("Please make sure all required fields are completed")
}

}[/html]Jared
Aug 7 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by RS | last post: by
2 posts views Thread by id santhosh via .NET 247 | last post: by
reply views Thread by jack | last post: by
9 posts views Thread by preeto50 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.