Validate With Regular Expressions
Regular expressions are the best tool for the job when you need to check user input against multiple formats.
by Bill Wagner
June 2003 Issue
Technology Toolbox: VB.NET, C#, ASP.NET regular expression validator
Simple validations often aren't adequate for some kinds of user input—for example, credit card numbers and Social Security numbers. The fact that users like to enter data in different formats complicates matters. For example, users might enter a credit card number as 1234 5678 9012 3456, 1234567890123456, or 1234-5678-9012-3456. You can parse any of these as a valid credit card number. Even simple requirements, such as ZIP codes, have a regular format, but some users might include spaces.
I'll show you how to use regular expressions to validate user input against a variety of common formats. You can find many of these formats in the ASP.NET regular expression validator wizard (see Figure 1 and Additional Resources). I'll start by showing you how to create a validator for credit card numbers in all possible formats. Then, you can create a WinForms application that lets you enter a possible credit card number or domain name and click on a button to validate it (see Figure 2, and download the source code).
Your goal is to create a regular expression you can use in the RegularExpression.IsMatch() method. This method returns TRUE if the method is a match, and FALSE if it's not. To match a regular expression, you create a regular-expression string that matches the expected input string. The key is that regular expressions provide a language to describe pattern matches. As a simple example, *.doc in the DOS dir command matches all Word documents. You can create similar and far more complex patterns with regular expressions.
When you build any regular expression to validate input, you want to match the entire input expression. All your regular expressions should start with ^ to match the beginning of the input string and end with $ to match the end of the input string (see Table 1 for the most common RegEx elements for validating user input).
I'll show you how to build the regular expression that matches only the forms of a credit card number that I listed previously (see Listing 1). All the forms use four groups of four digits each. You use \d to match a single digit, so the regular expression ^\d$ matches exactly one digit. However, you want four digits for each credit card number group. Regular expressions use braces ({}) to describe how many times a pattern is repeated, so \d{4} matches four digits. You use the expression ^\d{16}$ to match a credit card number with no intervening spaces.
Back to top
|