Click here to Skip to main content
6,594,432 members and growing! (17,236 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Regular Expressions     Intermediate License: The Code Project Open License (CPOL)

Email ID Validation

By milton cse00

Email ID validation using regular expressions (Finite Automata example).
C#, Windows, .NET, Visual Studio, Dev
Posted:27 Apr 2006
Updated:8 May 2006
Views:66,796
Bookmarked:34 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 5.30 Rating: 4.08 out of 5
3 votes, 15.0%
1
1 vote, 5.0%
2
2 votes, 10.0%
3
3 votes, 15.0%
4
11 votes, 55.0%
5

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)

About the Author

milton cse00


Member
Name SYED MD. ABUL BASHAR
Email ID: miltoncse00@yahoo.com

I am now working as software engineer in Bangladesh, my mother land; 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.


B.Sc(Engr) in Computer Science and Engineering CGPA-3.64 (max 4.0)
First Class and Secured 7th place Rajshahi University of Engineering and Technology 2004

Occupation: Software Developer
Location: Bangladesh Bangladesh

Other popular Algorithms & Recipes articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralPoor article. Example of validation in real world. Pinmembershaoun100021:59 28 Dec '08  
GeneralRe: Poor article. Example of validation in real world. PinmemberThe JZ4:12 6 Jan '09  
GeneralRe: Poor article. Example of validation in real world. Pinmembermilton cse0023:57 8 Jan '09  
GeneralRe: Poor article. Example of validation in real world. Pinmembershaoun10006:25 23 Jun '09  
GeneralNot working.... Pinmemberjasysam23:33 24 Sep '08  
GeneralRe: Not working.... Pinmembermilton cse0023:59 8 Jan '09  
GeneralHow do I validate internet (i.e Attach File path) PinmemberDhaval Pairkh11:00 23 Jul '07  
Generalvalidation of email account PinmemberQuickDeveloper21:47 27 Apr '06  
GeneralRe: validation of email account PinmemberVasudevan Deepak Kumar3:05 28 Apr '06  
GeneralRe: validation of email account Pinmembermilton cse0020:00 14 May '07  
GeneralRe: validation of email account Pinmembershaoun100019:04 16 Dec '08  
GeneralRe: validation of email account Pinmemberadima_764:45 19 Jun '07  
GeneralToo Simple PinmemberRobertAn13:52 27 Apr '06  
GeneralRe: Too Simple PinmemberJake Wharton16:28 27 Apr '06  
GeneralRe: Too Simple Pinmemberchmod75517:25 27 Apr '06  
GeneralRe: Too Simple Pinmemberjrandomuser6:08 8 May '06  
GeneralRe: Too Simple PinmemberMark Cranness20:50 15 May '06  
GeneralRe: Too Simple PinmemberCd-MaN22:01 15 May '06  
GeneralRe: Too Simple Pinmemberwinart21:54 17 May '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 May 2006
Editor: Smitha Vijayan
Copyright 2006 by milton cse00
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project