Click here to Skip to main content
13,355,018 members (58,482 online)
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 27 Apr 2006

Email ID Validation

, 8 May 2006
Rate this:
Please Sign up or sign in to vote.
Email ID validation using regular expressions (Finite Automata example).


Regular Expressions are very much useful for validation checking. It's not a new technology; it originated in the UNIX environment, and is commonly used with the Perl language. Regular expressions are, however, supported by a number of .NET classes in the namespace System.Text.RegularExpressions.

Its rules are same as the finite automata. Information regarding the main special characters or escape sequences that you can use are available in the MSDN.

Regular Expressions for Email Checking

Basic things to be understood in RegEx are:

  • “*” matches 0 or more patterns.
  • “?” matches single character.
  • “^” for ignoring matches.
  • “[]” for searching range patterns.

The rules for validating email IDs, and some valid and invalid examples are mentioned here:

  1. Email addresses must be start with a letter symbol. And any number of letters or digits or underscore (_) can be appended, and only a single dot (.) is allowed but other symbols and white spaces are not allowed.
  2. The name field of the address must end with either a letter or digit.
  3. If underscore or dote is used then before it, letters or digits must be used for a valid name.
  4. Dot can be used only once but underscore can be used multiple times.

Some examples:            valid              invalid
milton           invalid(white space)             valid          valid         invalid(_ before @)      valid     invalid(double dote)   valid  valid(dote before @)      
miltoncse00                      name portion

According to these rules and valid examples, we can draw a state diagram for valid name checking of email addresses:

Fig: state diagram for the naming portion

From the state diagram, the regular expression for the naming part is:


The rules for the email name portion (before @) can start with a letter. And any number of letters or digits can be appended and other symbols are not allowed.

So the regular expression for that part is:


After the dot (.) portion like (.com/.net), it can start with a letter and any number of letters or digits can be appended. If another dot portion is allowed then that can follow the same rule.

So the regular expression for that part is:


Combining all these regular expression, the regular expression for email checking that satisfies the Yahoo! email rules will be:


The C# code that can find that matching is very simple, as illustrated bellow:

string pattern=@"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|" + 
               @"0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z]" + 
System.Text.RegularExpressions.Match match = 
    Regex.Match(txtEmail.Text.Trim(), pattern, RegexOptions.IgnoreCase);


So, we conclude that any validation problems that involve recursion, option, limitation is easier to solve with regular expressions than using other ways (like if-elseif-else, while condition). This can be represented in a state diagram that is very much easier and efficient to express and use.

My next article will be on auto ID generation for any table using stored procedures.


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


About the Author

Software Developer
Bangladesh Bangladesh
Email ID:

I am now working as software engineer in Malaysia. I am from Bangladesh and I have completed my B.Sc (Engg.) in CSE from Rajshahi University of Engineering and Technology (RUET).I spend much time in learning latest technology.

My LinkedIn Profile :[^]

My blog :[^]

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Member 924149811-Jul-12 9:06
memberMember 924149811-Jul-12 9:06 
GeneralMy vote of 5 Pin
Jariwala Amin20-Feb-12 19:27
memberJariwala Amin20-Feb-12 19:27 
GeneralMy vote of 5 Pin
Member 436607127-Jan-12 21:34
memberMember 436607127-Jan-12 21:34 
GeneralMy vote of 5 Pin
emreinchains4-May-11 6:06
memberemreinchains4-May-11 6:06 
Generalgood article Pin
k4s17-Jan-11 6:57
memberk4s17-Jan-11 6:57 
GeneralMy vote of 4 Pin
maxpayne3916-Sep-10 1:51
membermaxpayne3916-Sep-10 1:51 
GeneralPoor article. Example of validation in real world. Pin
shaoun100028-Dec-08 21:59
membershaoun100028-Dec-08 21:59 
GeneralRe: Poor article. Example of validation in real world. Pin
The JZ6-Jan-09 4:12
memberThe JZ6-Jan-09 4:12 
GeneralRe: Poor article. Example of validation in real world. Pin
milton cse008-Jan-09 23:57
membermilton cse008-Jan-09 23:57 
GeneralRe: Poor article. Example of validation in real world. Pin
shaoun100023-Jun-09 6:25
membershaoun100023-Jun-09 6:25 
GeneralNot working.... Pin
jasysam24-Sep-08 23:33
memberjasysam24-Sep-08 23:33 
GeneralRe: Not working.... Pin
milton cse008-Jan-09 23:59
membermilton cse008-Jan-09 23:59 
I even not use my regular expression that I have given here.. I use RFC 2822 standard. In this article I just wanted to show how can we make regular expression from finite automata. I made it simple and more specific to yahoo id rule.. this rule is not for hotmail or gmail.. so how can I use this as a standard and general..
QuestionHow do I validate internet (i.e Attach File path) Pin
Dhaval Pairkh23-Jul-07 11:00
memberDhaval Pairkh23-Jul-07 11:00 
Generalvalidation of email account Pin
QuickDeveloper27-Apr-06 21:47
memberQuickDeveloper27-Apr-06 21:47 
GeneralRe: validation of email account Pin
Vasudevan Deepak Kumar28-Apr-06 3:05
memberVasudevan Deepak Kumar28-Apr-06 3:05 
GeneralRe: validation of email account Pin
milton cse0014-May-07 20:00
membermilton cse0014-May-07 20:00 
GeneralRe: validation of email account Pin
shaoun100016-Dec-08 19:04
membershaoun100016-Dec-08 19:04 
GeneralRe: validation of email account Pin
adima_7619-Jun-07 4:45
memberadima_7619-Jun-07 4:45 
GeneralToo Simple Pin
RobertAn27-Apr-06 13:52
memberRobertAn27-Apr-06 13:52 
GeneralRe: Too Simple Pin
Jake Wharton27-Apr-06 16:28
memberJake Wharton27-Apr-06 16:28 
GeneralRe: Too Simple Pin
chmod75527-Apr-06 17:25
memberchmod75527-Apr-06 17:25 
GeneralRe: Too Simple Pin
jrandomuser8-May-06 6:08
memberjrandomuser8-May-06 6:08 
GeneralRe: Too Simple Pin
Mark Cranness15-May-06 20:50
memberMark Cranness15-May-06 20:50 
GeneralRe: Too Simple Pin
Cd-MaN15-May-06 22:01
memberCd-MaN15-May-06 22:01 
GeneralRe: Too Simple Pin
winart17-May-06 21:54
memberwinart17-May-06 21:54 

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
Web03 | 2.8.180111.1 | Last Updated 8 May 2006
Article Copyright 2006 by Syed BASHAR
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid