![]() ![]() | ||
You use a RegularExpressionValidator control to check if the value in a data-entry control matches a pattern defined by a regular expression. You use regular expressions to see if text matches a certain pattern, which is a great way to check if the user has entered text in the way you want. Unfortunately, regular expressions are not easy to work with. There's an entire chapter in the Coriolis Perl Black Book dedicated to showing how regular expressions work.
In general, regular expressions are made up of text with embedded codes that start with a backslash (\) as well as other control codes. For example, the code for a word boundary (where a word ends or starts) is \b, and a "character class" is a set of characters surrounded with [ and ] that lets you specify what characters you want to accept, so this regular expression will match a word made up of uppercase and/or lowercase letters only (the + stands for "one or more of", so we're matching one or more uppercase and/or lowercase letters here):
\b[A-Za-z]+\b
That's hardly scratching the surface of regular expressions, though; for example, here's the regular expression Visual Basic uses to determine if text matches a valid email address—\w stands for a word character (such as letters and underscores and so on), and * means "zero or more of":
\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
As you can see, regular expressions can get pretty complex fairly quickly. Fortunately, Visual Basic includes some pre-built regular expressions you can use to match well-known sequences of characters, such as social security numbers, email addresses, telephone numbers, postal codes, and so on. There's a regular expression validator next to the email address text box—the fourth text box—in the ValidationControls example on the CD-ROM that you see in Figure 18.2 using precisely the above expression to check for valid email addresses.
![]() ![]() | ||