HTML and CSS Reference
<input type="text" name="userAge" id="userAge" />
<input type="submit" value="Send information" />
Save the file as formvalidation.html and view it in the browser. Figure 14.18 shows the
form in the browser.
file displayed in the
Feel free to click the submit button. You will notice that the inputs will be submitted.
At the moment we have not coded the validateForm() function, so the form simply
Accessing form inputs is a little tricky. The form is a property of the document object.
Each form element is a property of the form object. A property of a form element can
be a value. So, accessing the contents of an input box could look something like the
The form is identified by forms to indicate which form will be used. An XHTML
document can contain multiple forms. Note that there is an “s” in forms . The first
form is forms . A form could have a name attribute, but Strict XHTML does not
allow form names to be used directly as a property. So we'll err on the side of caution
and use the strict specification, forms to indicate which form we need to use. To
access the value in the userAge input box, we will need to use
document.forms.userAge.value . This is a mouthful, for sure.
Also, notice that the values true and false are not enclosed in quotes. This is impor-
keywords and represent special values. If you add quotes to them, they become strings
and this function will not work properly.