Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
This section does not provide a tip, but rather offers an example of how to use the validation functions in a form. Listing 24.17 is an HTML page with a form containing three input elements: Company, ProductID, and ProductQuantity. As you may have guessed, this form enables customers to order products. The Company, ProductID, and ProductQuantity boxes must not be left empty. In addition, ProductQuantity can contain only a number. Note that the HTML page includes a <SCRIPT> section with three functions: trim, isPositiveInteger, and validateForm. The validateForm function is assigned as the handler for the form’s onSubmit event. It is called just before the form is submitted. The validateForm function will return true only if the three boxes have valid values. If the validateForm function returns false, the form is not submitted. If one of the three form elements has an invalid value, an alert box is issued and the focus shifts to the box whose value is invalid.