JavaScript - Single Field Validation
JavaScript provides an easy way to validate single fields on an HTML
form. The Guest Book done earlier in this
course is an example of using JavaScripts.
JavaScript provides an easy way to do the following types of validation:
- test for Null
- integer range checking
- Min or Max text length checking
- special character checks - like EMAIL addresses
- specific list of valid values
ADVANTAGES:
- Integrates nicely with HTML form "Input Type" components like text,
checkbox, and button.
- Extends built in functionality of HTML components while maintaining the
same look and feel.
- Script is embedded in same page that is used to collect the data.
- Simple to program and understand.
DISADVANTAGES:
- Difficult to validate each field as the data is entered. "onBlur"
event has problems with this.
- No events to capture each keystroke for extremely tight validation.
- Limited to HTML components.
- Common functions must be replicated on each page.
See the following examples to see how this works.
JAVAScript Example 1 |
Demonstrates checking for required and optional fields, min and max lengths, and
a special character check. |
JAVAScript Example 2 |
Demonstrates
alpha character checks and numeric checks. |