Handling input from forms in JavaScript

Probably the most frequent way of interacting between web client and web browser is through forms. We may use forms to allow users to create queries which search through a large collection of data on the servers, or for the user to specify values of input that may contribute to a dataset held on the server.

In either case we may be interested in assuring that the data supplied by the user is checked for accuracy and/or consistency prior to submission of the data to the server. The examples provided here could be used to intercept the submission with a JavaScript function before the data is sent, though in each of these cases we are not actually invoking a server-side script.

Example 1: We wish to assure that all essential fields in a form are filled out. Perhaps the script running on the server automatically appends submitted data to files held on the server. We do not wish to have useless data clog up the database, so it makes sense to validate the data prior to submission.

* denotes required field
* favorite word   first name
last name * userid

Example 2. We use a form element of type select to control the possible values the user submits. Possible reasons for this may include wishing to prevent typos or out of range values.

Example 3. We use controlled vocabulary to validate the data.

Please enter the month of your birth