7.5. Input Fields and JiT Regular ExpressionsMost 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:
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
Certificates of purchase and warranty numbers may have a pattern that requires certain letters and/or numbers to appear in certain positions. As an example, if you have a certificate identifier that is 13 characters long, with the characters BUS in the sixth through eighth position, and alphanumeric characters in the remaining spots, you might try the following regular expression: ^\w{5}BUS\w{5} If you're validating an email address, which requires an amphora (at symbol), some form of domain, and little other restriction, the following should work: ^.+@[^\.].*\.[a-z]{2,}$ As for date, the following could work if you want a date in the format mm/dd/yyyy: ^\d{2}\/+\d{2}\/+\d{4} Examples too simple so far? Well, check out the following for Social Security numbers: ^(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}$ I'm so whizzy at regular expressions! Well, actually, I'm not very good at regular expressions. When I need to have one that's more complicated than dates or perhaps email addresses, I go shopping online by searching for "regular expression" with whatever it is I'm trying to match against. In Example 7-4, the format validated against was my own (well, I devised; others have probably used the same pattern), and was a simple regular expression that ensures only that the appropriate number of digits are given, that the characters are only digits, and that each grouping is of the right sizeall separated by dashes. Which, if you think about it, covers quite a bit. Compare that, though, with the regular expression I just provided, created by Michael Ash and courtesy of the Regular Expression Library (an invaluable resource at http://regexlib.com/). This not only validates against the format, it also validates against what is known about Social Security numbersthe number groupings and so on. There are others at least as complex that can differentiate between a Visa credit card and a MasterCard.
On the other hand, do you need to differentiate between Visa and MasterCard? The important point to remember about regular expressions is that you can get carried away trying to find the perfect validation pattern, spending more time than the validation is worth. You have to weigh your time against how important it is to validate the entry before submitting it to the server. Speaking of which, that's just about enough time on events, forms, and JiT validation. Time to move on to Chapter 8: JavaScript's roots, cookies, and evil things that go bump in the browser.
|