One of the more important uses of JavaScript is to validate user input prior to sending the data to a receiving program at the server end. This is significant for several reasons:
- It reduces load on the server. Invalid data are already filtered out when input is passed to the program.
- The user gets quicker responses, as the validation is performed on the client-side. The data does not have to be sent back for correction.
- It simplifies the server-based program.
Basically, an author can choose to validate input in two levels:
- at the field-level, as the user inputs the data, by assigning a handler to either:
- the onchange event attribute of each form element that you want validated;
- any of the keyboard events attribute possible for the given field.
- at the form-level, when the user submits the form, by assigning a handler to either:
- the onclick event attribute of the button that submits the form;
- the onsubmit event attribute of the form itself.
This section lists the basic set of form validation routines that are covered in this workshop:
Web Forms Validation