Website Development
JavaScript Exercise · 3 Extra Credit Points


Form Validation

Write validation functions and modify the onSubmit event Handler in the form code to validate the following form fields:

  1. Member number
  2. Password

If an error occurs, use an alert box to display an error message for the first field in error and place the cursor in the first field in error.

Right click here and use Save Link As (or Save Target As) to download the HTML file (the form is illustrated below)

Be sure to test your code in both Internet Explorer and Firefox.


Form Image

Steps

  1. Add the code to put the cursor in the first field on the form (Member Number) when the page first loads
  2. Add the code to the form tag to run the function validateForm when the form is submitted.
  3. In the function validateForm(), change the initial value of the variable to false: var validForm=false;
  4. After the onSubmit event handler, but inside the quote marks, add the word return in front of the function call.
  5. In the function validateForm(), change the initial value of the variable back to true.
    Then remove the two slashes commenting out the validForm = validateNumber(); line of code.
    Make changes in the validateNumber() function to do the following. Refer to the form validation notes and example:
  6. Add an else statement after the closing curly bracket of the if statement in the function validateNumber(). Then do the following:
  7. Using the code written for the memberNumber field, add code to the password field to verify the following:
  8. Comment out the line in the validateForm function that displays the alert box and save your work.:
    //alert('In function validateForm. validForm = '+validForm);
    Refresh the Web page and enter all valid data in the two entry fields.

  9. To fix the above problem, make the following changes:
  10. An explanation of the code of the currentDate hidden field:
  11. Demonstrate the final Web page to your instructor.