<html>
<head>
<script language=javascript>
<!-- Hide from older browsers
function submitPage( form ) {
foundError = false;
// 1 Make sure the name field is filled in
if (form.Name.value == "" ) {
alert("You did not enter a Name.");
foundError = true;
}
// 2 Make sure the email field is not blank
if (foundError == false && (form.Email.value == "" )) {
alert("You did not enter an email address.");
foundError = true;
}
// 3 Make sure a region has been chosen
region = form.select1.selectedIndex
if (foundError == false && (form.select1.options[region].value == "" )) {
alert("Please select a Region.");
foundError = true;
}
// 4 Check for a plausible email address
if (foundError == false && (form.Email.value.indexOf ( '@',0 ) == -1)) {
alert("You did not enter a valid email address.");
foundError = true;
}
// 5 If all Fields are Valid, Write Verification Message
if (foundError == false) {
document.open( );
document.write("<html><head><title>Form Verification</title></head>");
document.write("<body bgcolor=#ffffff><center>");
document.write(" <br> <br>");
document.write("<h2>Thank you for your submission.</h2>");
document.write("</center></body></html>");
document.close( );
}
}
// End hiding -->
</script>
</head>
<body> <form action=" " name="jensForm" method="get"> Name : <input type="text" name="Name" size=30> (any format) Email : <input type="text" name="Email" size=30> (name@domain) <select name="select1"> <option value="" selected>Where are you from? <option value=West Coast>West Coast <option value=Mountains>Western Mountains <option value=Desert>Western Desert <option value=Alaska>Alaska <option value=Hawaii>Hawaii </select> <input type="button" name="submit" value="submit Form" onClick="submitPage(jensForm)"> <input type="reset" name="reset" value="reset"> </form> </body> </html> |
When the user clicks the "submit" button, the "submitPage(form)" function
is triggered. The "submitPage(form)" function consists of five parts:
1. Check for a blank name field
2. Check for a blank email field
3. Verify that a region has been selected
4. Check for a plausible email address
5. Write the user a verification message
Each of the first four parts is set up as a condition. The variable
"foundError" is set to "false" at the beginning of the script. If any of
the four conditions fails, "foundError" is changed to "true" and an alert
message is triggered. If "foundError" remains false once all of the
condtions have been tested, then the form is submitted and a verification
message is written to the user.