Click here to Skip to main content
Click here to Skip to main content

RegularExpressionValidator In ASP.NET

By , 8 Oct 2012
 

Introduction

Here, I show you the use of RegularExpressionValidator to validate various expressions like:

  1. EmailID
  2. U.S. Phone Number

Background

It is a common requirement for beginners in ASP.NET to write various validation expressions when creating a form to register a user.

It is necessary to specify that RegularExpressionValidator is validating which control. In the ControlToValidate property, we specify the control which will be validated. For this, we need to mention the ID of the textbox control.

RegularExpressionValidator for Email ID

Here, I have shown a textbox with ID="txtEmail".

  <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvEmail" runat="server" 
                ErrorMessage="*" ControlToValidate="txtEmail"
                    ValidationGroup="vgSubmit" ForeColor="Red"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" 
                runat="server" ErrorMessage="Please Enter Valid Email ID"
                    ValidationGroup="vgSubmit" ControlToValidate="txtEmail" 
                    CssClass="requiredFieldValidateStyle"
                    ForeColor="Red" 
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
                    </asp:RegularExpressionValidator>

RegularExpressionValidator for U.S. Phone Number

 <asp:TextBox ID="txtPhoneNumber" runat="
server"></asp:TextBox>(000-000-0000)
                <asp:RequiredFieldValidator ID="rfvPhoneNumber" runat="server" 
                ErrorMessage="*" ControlToValidate="txtPhoneNumber"
                    ValidationGroup="vgSubmit" ForeColor="Red"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
                runat="server" ErrorMessage="Phone Number is not valid"
                    ValidationGroup="vgSubmit" ControlToValidate="
                    txtPhoneNumber" ForeColor="Red"
                    ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}">
                    </asp:RegularExpressionValidator>

Points of Interest

These are the most common validation expressions used to validate an Email ID and phone number.

There is no way to check the false Email IDs. They can be checked by the mail servers only.

There is no way to check the false phone numbers. They are also checked by the Phone agencies.

These regular expressions only check the expression pattern for an email or a phone number.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sonali Agarwal
Web Developer
India India
Member
I am a web developer ready to work on challenging tasks.
My articles on codeproject:
1. http://www.codeproject.com/Tips/464243/Usage-of-multiple-Web-Services-in-a-single-web-app
2. http://www.codeproject.com/Tips/465600/Display-local-time-of-various-cities-in-world-usin
3. http://www.codeproject.com/Tips/465292/Save-image-with-printed-text-on-it

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberDrABELL9 Oct '12 - 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 9 Oct 2012
Article Copyright 2012 by Sonali Agarwal
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid