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
 

Getting Started with Regular Expressions : Page 4

Regular expressions, also referred to as "regex" in the developer community, are extremely powerful pattern matching and substitution tools. This article introduces you to regular expressions, what they are, why you would want to use them, and finally, how you can begin putting them to work in Visual Studio .NET.


advertisement

Figure 1: The properties for the regPhoneNumber RegularExpressionValidator control.
Using Regular Expressions in Visual Studio .NET
Now that you've seen the fundamentals of building a regular expression pattern, and you've reviewed a number of popular patterns, it's time to find out how to implement a regular expression in Visual Studio .NET.

Visual Studio .NET supports regular expressions in ASP.NET via the RegularExpressionValidator control and in code via the RegEx class.

ASP.NET
While there is more than one way to use regular expressions in ASP.NET, the most common is to use the RegularExpressionValidator control.

A developer uses the RegularExpressionValidator control to make sure the entry in a watched control conforms to a specified regular expression. This allows you to check an entry against a pattern such as a U.S. Social Security number, telephone number, e-mail address, etc.

The following snippet lists the HTML for a RegularExpressionValidator control watching over a textbox containing U.S. phone number information.

   <asp:RegularExpressionValidator 
      id="regPhoneNumber" 
      runat="server" 
      ErrorMessage=
            "Please enter a valid phone number!" 
      ControlToValidate="txtPhone" 
      ValidationExpression=
            "((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}">
   </asp:RegularExpressionValidator></P>
The Property window in Figure 1 shows the regPhoneNumber Control.



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