Validating Input with the RegularExpressionValidator


Many forms of user input must be entered in a certain format. For example, when we're asking a user for her email address, the provided email address must follow this particular format: one to many alphanumeric characters; the at symbol (@); one to many alphanumeric characters; period; top-level domain name, such as com, net, org, edu, us, uk, fr, and so on.

For the ValidationControlTestBed.aspx ASP.NET web page, users are asked for their Social Security number. In the United States, all citizens are given a Social Security number, which contains nine digits and is typically written in the form XXX-XX-XXXX

To ensure that a string input meets some specified format, we can use a RegularExpressionValidator. The RegularExpressionValidator uses regular expressions to determine whether the user's input matches the accepted pattern. A regular expression is a string that contains characters and special symbols and specifies a general pattern. Fortunately, you do not need to be well versed in regular expression syntax to be able to use the RegularExpressionValidator.

By the Way

Regular expressions are commonly used in a number of program domains and are definitely worth learning. However, an extensive examination of the topic is far beyond the scope of this book, especially because Visual Web Developer provides a number of built-in regular expression patterns that you can use in the RegularExpressionValidator without knowing a thing about regular expression syntax.

If you are interested in learning about regular expressions, I encourage you to read "An Introduction to Regular Expressions" at http://www.4guysfromrolla.com/webtech/090199-1.shtml and "Common Applications of Regular Expressions" at http://www.4guysfromrolla.com/webtech/120400-1.shtml. There's also an extensive regular expression repository and community at http://www.regexlib.com/.


To ensure that the Social Security number is inputted in a proper format, let's add a RegularExpressionValidator Web control to the ASP.NET page. Drag and drop this Web control from the Toolbox onto the designer, placing it immediately after the ssn TextBox Web control, as shown in Figure 12.18.

Figure 12.18. A RegularExpressionValidator has been added to the ASP.NET web page.


Set the RegularExpressionValidator's ControlToValidate property to ssn and its ErrorMessage property to Your social security number must be in the format XXX-XX-XXXX. After you set these two properties, the only other property you need to specify is the ValidationExpression property, which specifies the regular expression pattern that the user's input must conform to.

To edit this property, click on the ValidationExpression property; to the right you will see a pair of ellipses. Clicking on these ellipses will display the Regular Expression Editor dialog box (see Figure 12.19).

Figure 12.19. The Regular Expression Editor dialog box allows you to choose a predefined regular expression pattern.


The Regular Expression Editor dialog box contains a list of standard regular expression patterns that you can choose from. Alternatively, you can type a custom regular expression pattern into the Validation Expression text box. Because we need validation for a Social Security number, scroll down to the U.S. Social Security Number option.

After you have selected the U.S. Social Security Number option, the regular expression pattern will be displayed in the Validation Expression text box. Click the OK button to set the RegularExpressionValidator's ValidationExpression.

After you have done this, take a moment to view the ValidationControlTestBed.aspx ASP.NET page through a browser. Note that an error message is displayed if you provide a Social Security number that doesn't follow the correct format: three digits, hyphen, two digits, hyphen, four digits. For example, a legal social security number would follow this format: 123-45-6789.




Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net