Click here to Skip to main content
13,249,813 members (82,189 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 8 Oct 2012

RegularExpressionValidator In ASP.NET

, 8 Oct 2012
Rate this:
Please Sign up or sign in to vote.
ASP.NET RegularExpressionValidator


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

  1. EmailID
  2. U.S. Phone Number


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"

RegularExpressionValidator for U.S. Phone Number

 <asp:TextBox ID="txtPhoneNumber" runat="
                <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}">

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.


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 Essential Solve LLC
India India
I am flexible, responsive, creative and enthusiastic with ability to manage multiple initiatives with deadline. I have willingness to pick up and develop new skills and ability to balance a number of conflicting priorities and make decisions. I am results oriented - focused on productive and high-yield activities.

You may also be interested in...

Comments and Discussions

QuestionWhat about whitespace? Pin
TecNQ7-Feb-17 13:48
professionalTecNQ7-Feb-17 13:48 
Questioncode related Pin
Member 1242301629-Mar-16 1:25
memberMember 1242301629-Mar-16 1:25 
AnswerRe: code related Pin
vaynenick4-Sep-16 21:42
membervaynenick4-Sep-16 21:42 
Questionstrict Regular expression validation Pin
Dawood Abbas17-Nov-14 21:36
memberDawood Abbas17-Nov-14 21:36 
GeneralMy vote of 5 Pin
DrABELL9-Oct-12 8:34
memberDrABELL9-Oct-12 8:34 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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