O'Reilly logo

Programming ASP.NET 3.5, 4th Edition by Jesse Liberty, Dan Hurwitz, Dan Maharry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Regular Expressions

Often, a simple value or range check is insufficient; you must check that the form of the data entered is correct. For example, you may need to ensure that a zip code is five digits, an email address is in the form , a credit card matches the right format, and so forth.

A regular expression validator allows you to validate that a text field matches a regular expression. Regular expressions are a language for describing and manipulating text.

Tip

For complete coverage of regular expressions, see Mastering Regular Expressions, Third Edition, by Jeffrey E.F. Friedl (O’Reilly).

A regular expression consists of two types of characters: literals and metacharacters. A literal is a character you wish to match in the target string. A metacharacter is a special symbol that acts as a command to the regular expression parser. (The parser is the engine responsible for understanding the regular expression.) Consider this regular expression:

^\d{5}$

This will match any string that has five numerals. The initial metacharacter, ^, indicates the beginning of the string. The second metacharacter, \d, indicates a digit. The third metacharacter, {5}, indicates five of the digits, and the final metacharacter, $, indicates the end of the string. Thus, this regular expression matches five digits between the beginning and end of the line and nothing else.

Tip

When you use a RegularExpressionValidator control with client-side validation, the regular expressions are matched using ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required