Content area
Full text
Invariably, some visitors will input data incorrectly in HTML forms, so there's no getting around validating the input. You can use CGI for validation, but that requires the user to submit the form and wait until the server confirms the validation. I strongly suggest validating with client-side JavaScript, which allows incorrect input to be flagged to the user immediately.
Changes in JavaScript 1.2 mean that you can filter the input while the user is inputting data. For example, to ensure that a field accepts only numbers, you can create a filter that causes the field not to be affected if the user hits any keys except0-9. This is an improvement over JavaScript 1.1, in which you had to hit a button for a function to check the field for nonnumeric characters.
The Event Object
The event object exists only in JavaScript 1.2. Like other JavaScript objects, it contains properties. Each property describes a JavaScript event, and is passed as an argument to an event handler when the event occurs. For example, if the mouse Down event occurs in an image object, the event object will contain the type of event, the cursor position, and a field denoting the modifier key pressed at the time of the event. Table 1 lists all the event handlers in JavaScript 1.2, while Table 2 describes the event object's properties.
When you call a function explicitly from an HTML document, the event name must appear in lower case and the function must not end in parentheses. Example 1 shows an event object calling an event explicitly. In this example, the function passes e, which indicates the event.
Creating the Form
The first step is to determine what data is...





