Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Validating Data On Web Forms : Page 5

There are six validation controls included with ASP.NET as well as a summary control. Data validation with the ASP.NET validation controls is always processed on the Web server and can also optionally be processed on the user's computer.


advertisement
RegularExpressionValidator Control
You can compare the value entered into a control against a regular expression if you use the ASP.NET RegularExpressionValidator control. A regular expression is a series of characters that define a format expression that you can apply to a value. An overview of regular expressions is beyond the scope of this article. However, you can read about the fundamentals of regular expressions in "Getting Started with Regular Expressions" in the May/June 2003 issue of CoDe Magazine. You can also find a detailed discussion of regular expressions in the Visual Studio .NET Help file as well as in the MSDN library.

In this HTML snippet, the value entered into a textbox control is compared to a regular expression that was created to validate an e-mail address format.

<asp:TextBox ID="txtEmail" RunAt="Server" /> <asp:RegularExpressionValidator ControlToValidate="txtEmail" Display="Static" ErrorMessage="E-mail address is not formatted correctly." ID="revEmail" RunAt="Server" ValidationExpression="[\w\x2E]\x40{1}[\w\x2E] {2,}\x2E{1}[\w\x2E]{2,}" />

In the snippet above, I've linked the RegularExpressionValidator control to the txtEmail TextBox control using the ControlToValidate attribute. By now, the only attribute that isn't immediately obvious to you is the ValidationExpression attribute.

The ValidationExpression attribute defines the regular expression used to compare the value in the control to. You can see the results of the RegularExpressionValidtator control in Figure 5.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Thanks for your registration, follow us on our social networks to keep up-to-date