Pages

Saturday, April 26, 2008

RegularExpressionValidator Control

Regular expressions are an extremely powerful means of quickly parsing text. Regular expressions use an extensive pattern-matching notation to provide searching, replacing, and other text operations quickly and efficiently. The pattern-matching capabilities of regular expressions are of particular interest when using the RegularExpressionValidator control.

Regular-expression validation is generally used to ensure that specified data matches a predetermined pattern. For example, all e-mail addresses follow a common pattern. The regular expression used for comparison is specified with the ValidationExpression property. This property can contain any valid regular expression. Relying once again on the example of validating an e-mail address, you can use the following regular expression.

\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
It is pretty obvious that the regular expression syntax is quite cryptic. Fortunately, there are libraries of regular expressions available. In fact, the RegularExpressionValidator control has some predefined regular expressions (including e-mail address). You can always, of course, write your own regular expressions for validation. Suppose, for example, you have a custom purchase order number format. You can use a regular expression to validate data in the purchase order number field against the pattern you define.

No comments: