Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

Validating Forms with JavaScript 529

19


Exercise 19.1: Form Validation ▼


Now take a look at how the registration form is validated with JavaScript. Whenever you
click the Submit button on a form, two events are triggered: the onclick event for the
button and the onsubmit button for the form. For form validation, onsubmit is the better
choice because some forms can be submitted without clicking the Submit button. When
the onsubmit event is fired, the validation function is called.


First open the HTML for your form. It looks like this:


<!DOCTYPE html>




Registration Form


Free download pdf