When a user submits this form, the JavaScript code in the background checks to make sure
that the Name text box was filled in. When filled out correctly, with the name “Steve”, for example, the page displays the entered name, as shown in Figure 14-2.
Figure 14-2 When the web form is filled out correctly, the Name text box displays a greeting.
If a user fails to enter any data in the Name text box, the script displays an alert() dialog box
indicating that the field is required, as you can see in Figure 14-3.
Figure 14-3 The form displays an alert when the Name text box is empty.
Chapter 14 Using JavaScript with Web Forms
277
The code that does all this follows. You can find it in the formvalid.htm file in the companion
content. The file includes the Hypertext Markup Language (HTML) shown here:
"http://www.w3.org/TR/html4/strict.dtd">
A Basic Example
A Basic Form Example
The JavaScript within the element first links to the event handler script ehandler.js,
which was developed in Chapter 11, “JavaScript Events and the Browser.” Next, it defines a
function called formValid() to process the input from the simple form, as shown in this code:
function formValid(eventObj) {
if (document.forms[0].textname.value.length == 0) {
Within the formValid() function, an if conditional test uses the document.forms[] array. By
examining the first index value (0) of that array, the code finds the only form on this webpage.
The conditional tests whether the length of the textname.value property on the form is 0. If it
is, the script indicates the error using an alert() dialog box. If it is not, it displays whatever is in
the textname.value property.
The return value is important. When the submit or click event handlers are called and return
false, the browser halts the form submission process. This is why returning false is important
when validation fails. Without returning false, the default action is to continue and submit the
form. You can stop the default action in most browsers by calling the preventDefault() method. However, preventDefault() is not available in Windows Internet Explorer prior to version
9, so the script executes a conditional test to see first whether the preventDefault() method
is available. If it is, the script calls preventDefault(); otherwise, the script sets the returnValue
property of the window.event object to false to account for Internet Explorer.
The next bit of JavaScript, which appears in the HTML element, adds the submit
event to the form by using the EHandler event handler script:
var formEl = document.getElementsByTagName("form")[0];
Note that to retrieve the form, the formValid() function uses the first index value of the
document.forms[] list, whereas the var formEl definition uses the getElementsByTagName
method. Both of these approaches work fine when only one form is on the page. You’ll also
frequently see script that accesses the form through its name, as shown in the next section.
Obtaining Form Data
Before you can provide feedback based on the form data, you have to get access to it. The
previous example showed how to access the form data using the document.forms[] array and
the getElementsByTagName function. This section shows a different way to perform the same
operation—using the form’s name property instead of its index.
Like other elements of an HTML page, you can set the id attribute for a form element. Here’s
the previous example with an id attribute:
Chapter 14 Using JavaScript with Web Forms
279
You can then access the form using its name rather than its index, as follows:
document.forms["testform"]
Using name is useful, because in some cases, you might not know the index value of the form
you want to access, which happens sometimes when server-side or client-side code creates a
form dynamically, and you have to figure out (or worse, guess) the index value of the specific
form you need within the document. The most consistent way to ensure you can get a reference to the index value is to set the form’s id and then access it through that id. You can also
access the form directly in a nonstandard way, through the document object, but I wouldn’t
recommend it:
document.testform
This direct approach doesn’t work consistently across browsers, and it’s not that much more
effort to type it correctly anyway, like this:
document.forms["testform"]
Working with Form Information
You can access all individual elements of web forms through the DOM. The exact method for
accessing each element differs depending on the type of element. For text boxes and select
boxes (also known as drop-downs), the value property holds the text that a visitor types in or
selects. You use a somewhat different approach from value to determine the state of radio
buttons and check boxes, though, which this section explains.
Working with Select Boxes
A select box holds groups of options. Here’s an example of the HTML used to create a select
box (which you can find in the selectbox.txt file in the companion content).
This code produces a select box like the one shown in Figure 14-4.
Figure 14-4 A select box based on the HTML example.
When a user selects an option, the select box’s value property is set to the value of the
particular option chosen. For this example, the select box named startype holds in its value
property whatever the visitor selects. You can access this property as follows:
document.forms["starform"].startype.value
For this particular example, you need to connect an event handler to the change event of the
select box, which you can do with the help of the EHander event handler script developed
in Chapter 11. The change event triggers a function each time the selection in the select box
changes, such as when the user selects an option using the drop-down menu. The page attaches the change event to the