Most form fields just require some text without giving any concern to the format. However, certain types of fields may require a specific format. Rather than send the data across to the server to see if the data is valid, we’ll use regular expressions to validate the format of the data, at a minimum, first.
Using regular expressions, as defined in Chapter 3, some of the more common validations are with the following fields:
Warranty or purchase certificates
Social Security numbers or other forms of identification
Credit card numbers
Web page URLs or other forms of URI (uniform resource identifiers)
Rather than try out various regular expressions directly in code, Example 7-5 contains a little application, the JiT RegEx Machine, that takes a regular expression typed in one field, a string in another, and then does a pattern match when the form is submitted. The results are output to a third field.
Example 7-5. The JiT RegEx Machine application