The RegularExpressionValidator Server Control in asp.net


The RegularExpressionValidator Server Control:

One exciting control that developers like to use is the RegularExpressionValidator control. This control offers a lot of flexibility when you apply validation rules to your Web forms. Using the RegularExpressionValidator control, you can check a user’s input based on a pattern that you define using a regular expression.

For instance, you can define that the structure of the user input must be in the form of an e-mail address or an Internet URL; if it doesn’t match this definition, the page is considered invalid.

 

Making sure the text-box value is an e-mail address

Email:

<asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
  &nbsp;
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
  Runat="server" ControlToValidate="TextBox1"
  ErrorMessage="You must enter an email address"
  ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>

Visual Studio 2005 makes it a little easier to use regular expressions by introducing the Regular Expression Editor. This editor provides a few commonly used regular expressions that you might want to apply to your RegularExpressionValidator. To get at this editor, you work with your page from Design view. Be sure to highlight the RegularExpressionValidator1 server control in this Design view to see the control’s properties. In the Property window of Visual Studio, click the button found next to the ValidationExpression property to launch the Regular Expression Editor.

Using this editor, you can find regular expressions for things like e-mail addresses, Internet URLs, zip codes, phone numbers, and social security numbers. In addition to working with the Regular Expression Editor to help you with these sometimes complicated regular expression strings, you can also find a good-sized collection of them at an Internet site called RegExLib found at www.regexlib.com

Other Related Posts:

Validations:

ASP.NET Validation Server Controls

The RequiredFieldValidator Server Control in asp.net

Validating Drop-Down Lists with the RequiredFieldValidator Control

The CompareValidator Server Control in asp.net

The RangeValidator Server Control in asp.net

The RegularExpressionValidator Server Control in asp.net

The CustomValidator Server Control in asp.net

The ValidationSummary Server Control in asp.net

Advertisements

One thought on “The RegularExpressionValidator Server Control in asp.net

  1. Pingback: Regularexpressionvalidator email | Figifoto

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s