In this lab you will write your own functions to grade an online quiz and
validate required inputs for a form. The
HTML file that contains the form with the quiz
is given to you. This file links to a JavaScript file, code.js,
which you can download here. Notice that
code.js is only a template, which we will fill in today.
We will write JavaScript code that:
form.html file you just downloaded,
and make note of the naming conventions. Here's one helpful hint: in the Web developer toolbar, select Information > Display ID and Class Details. That will show you this window: 
_recipient input, so that the form will be mailed to your own email address.
code.js file. You will add all the functions
to validate the form and grade the quiz here.
In code.js, define the gradeQuiz() function that
grades the quiz.
The area to the right of the button
is called quiz_result (quiz_result is the ID of that area of the page). Here is the code that creates quiz_result alongside the form itself:
|
|
The score of the quiz will be shown next to the
button, like this:
| One answer correct |
|
| Two answers correct |
|
| All three answers correct |
//grade the (check-box) third question
if ((document.quizForm.q3[1].checked) && (document.quizForm.q3[2].checked) &&
(!document.quizForm.q3[0].checked)) {
tally = tally + 1;
}
The function presents the result on the page, by setting the innerHTML
property of the quiz_result span, like:
document.getElementById("quiz_result").innerHTML = " Perfect Score!";
gradeQuiz() is called
when the user clicks on
.
In code.js, define a resetForm() function that
resets each question in the form to its initial state
(i.e., red asterisks and initial message restored).
document.getElementById("name_asterisk").style.visibility="visible";
document.getElementById("quiz_result").innerHTML = "";
The event handler for the Reset
button calls this function.
In code.js, define a validForm() function that
validates the two required inputs: the user's name and whether they
have taken a CS course.
When the user clicks on the Submit
button, validForm() is called.
Our validation will be modular: We will
define separate helper functions to validate each type of question.
The validForm()
function will just call the helper functions.
The following defines a valid input:
Name text input is valid if not empty:
if (document.quizForm.studentName.value != "") {
document.getElementById("name_asterisk").style.visibility = "hidden";
return true;
}
Have you taken a CS course question is valid
if a radio button has been checked:
if (document.quizForm.course_taken[0].checked ||
document.quizForm.course_taken[1].checked) {
document.getElementById("course_asterisk").style.visibility
= "hidden";
return true;
}
Reset button
should change to a personalized message which uses the name entered on the form
(i.e. "Thanks for the info, Wendy!"):
var validName = validateText();
var validCourse = validateRadio();
if (validName && validCourse) {
document.getElementById("form_status").innerHTML =
" Thanks for the info, " +
document.quizForm.studentName.value + " !";
return true;
}
Finally, the contents of the form should be emailed to your email address.
Here's a link to a working version of the form, and a link to the JavaScript code, with all the functions defined.