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

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).

Introduction

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:

miltoncse00@yahoo.com            valid
2milton00@yahoo.com              invalid
milton cse00@yahoo.com           invalid(white space)
milton_cse@yahoo.com             valid
milton_cse_00@yahoo.com          valid
milton_cse_00_@yahoo.com         invalid(_ before @)
milton.case_00_00@yahoo.com      valid

milton.cas.e_00_00@yahoo.com     invalid(double dote)
milton.cas.e_00_00@yahoo.co.in   valid
milton.cas.e_00_00.@yahoo.co.in  valid(dote before @)
miltoncse00@yahoo.com      
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:

[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?

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:

[a-z][a-z|0-9|]*

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:

([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)

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

^[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][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$

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]" + 
               @"[a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
System.Text.RegularExpressions.Match match = 
    Regex.Match(txtEmail.Text.Trim(), pattern, RegexOptions.IgnoreCase);

if(match.Success)
    MessageBox.Show("Success");
else
    MessageBox.Show("Fail");

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.

License

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

Share

About the Author

Syed BASHAR
Software Developer
Bangladesh Bangladesh
Name SYED MD. ABUL BASHAR
Email ID: miltoncse00@gmail.com
 
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 : http://bd.linkedin.com/in/miltoncse00[^]
 
My blog :http://ciintelligence.blogspot.com/[^]

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 924149811-Jul-12 8:06 
GeneralMy vote of 5 PinmemberJariwala Amin20-Feb-12 18:27 
GeneralMy vote of 5 PinmemberMember 436607127-Jan-12 20:34 
GeneralMy vote of 5 Pinmemberemreinchains4-May-11 5:06 
Generalgood article Pinmemberk4s17-Jan-11 5:57 
GeneralMy vote of 4 Pinmembermaxpayne3916-Sep-10 0:51 
GeneralPoor article. Example of validation in real world. Pinmembershaoun100028-Dec-08 20:59 
Hello,
Your article is too poor for validating an email id. Here i am showing an example how to validate email id. You can verify email id here:

Verify email id

 
This example can explain you how you can validate an email id in real world. This is just an example. Not a tutorial. I will write an article upon it later.
 
Thank you all.
GeneralRe: Poor article. Example of validation in real world. PinmemberThe JZ6-Jan-09 3:12 
GeneralRe: Poor article. Example of validation in real world. Pinmembermilton cse008-Jan-09 22:57 
GeneralRe: Poor article. Example of validation in real world. Pinmembershaoun100023-Jun-09 5:25 
GeneralNot working.... Pinmemberjasysam24-Sep-08 22:33 
GeneralRe: Not working.... Pinmembermilton cse008-Jan-09 22:59 
QuestionHow do I validate internet (i.e Attach File path) PinmemberDhaval Pairkh23-Jul-07 10:00 
Generalvalidation of email account PinmemberQuickDeveloper27-Apr-06 20:47 
GeneralRe: validation of email account PinmemberVasudevan Deepak Kumar28-Apr-06 2:05 
GeneralRe: validation of email account Pinmembermilton cse0014-May-07 19:00 
GeneralRe: validation of email account Pinmembershaoun100016-Dec-08 18:04 
GeneralRe: validation of email account Pinmemberadima_7619-Jun-07 3:45 
GeneralToo Simple PinmemberRobertAn27-Apr-06 12:52 
GeneralRe: Too Simple PinmemberJake Wharton27-Apr-06 15:28 
GeneralRe: Too Simple Pinmemberchmod75527-Apr-06 16:25 
GeneralRe: Too Simple Pinmemberjrandomuser8-May-06 5:08 
GeneralRe: Too Simple PinmemberMark Cranness15-May-06 19:50 
GeneralRe: Too Simple PinmemberCd-MaN15-May-06 21:01 
GeneralRe: Too Simple Pinmemberwinart17-May-06 20:54 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 8 May 2006
Article Copyright 2006 by Syed BASHAR
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid