.NET Framework Bookmark and Share   
 index > Regular Expressions > Email address regular expression
 

Email address regular expression

I used the function below to validate email addresses.

// Function to test for valid email address.
public static bool IsValidEmailAddress(String emailAddress)
{
Regex regex = new Regex(@"^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$");
Match match = regex.Match(emailAddress);
return match.Success;
}

This regular expression works for amy.charles@test.com, but not robert.o'neil@test.com.

Can anyone help me to modify the regulation expression, making it to valid for robert.o'neil@test.com

Thanks in advance!
waterding
Read this as it discusses that very subject under the heading "Trade-Offs in Validating Email Addresses"
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove
Email address validation is (IMHO) a worthless task! Check out the RFC standard. I suggest that you make sure you have [^@]+@.* that is the best one. Check out the forum post on why most regex's fail:

How to: Verify That Strings Are in Valid E-Mail Format

Phil Haacks comments:

I Knew How To Validate An Email Address Until I Read The RFC

and finally the RFC on wikipedia if you are still brave:

E-mail address

:-)

William Wegerson (www.OmegaCoder.Com)
OmegaMan
Read this as it discusses that very subject under the heading "Trade-Offs in Validating Email Addresses"
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove
Email address validation is (IMHO) a worthless task! Check out the RFC standard. I suggest that you make sure you have [^@]+@.* that is the best one. Check out the forum post on why most regex's fail:

How to: Verify That Strings Are in Valid E-Mail Format

Phil Haacks comments:

I Knew How To Validate An Email Address Until I Read The RFC

and finally the RFC on wikipedia if you are still brave:

E-mail address

:-)

William Wegerson (www.OmegaCoder.Com)
OmegaMan
Below link will provide complete reference to solve your issue:

http://www.codeproject.com/KB/validation/Valid_Email_Addresses.aspx




Rohini Chavakula
Rohini,
I just tried the pattern you said was a "complete reference to solve" the issue.

string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(strRegex);
if (re.Match("robert.o'neil@test.com").Success)
   Console.WriteLine("Success");
And it didn't match. I would listen to William's advice. He is an expert in Regex.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove
And it didn't match. I would listen to William's advice. He is an expert in Regex.

I do ok...but the thought is about the email pattern is that, if a user double validates the email address why question what they believe is the real one? Unless you are creating a new emaill address for a email box...what is the real point of true validation.... By simply checking for characters before a @ sign and then a .ABC it should be a valid one.


William Wegerson (www.OmegaCoder.Com)
OmegaMan

You can use google to search for other answers

Custom Search

More Threads

• Any Special Characters Need Backslash Within Brackets?
• Regex pattern
• Use RegularExpressionValidator not allowing certain characters
• extracting data from html
• Regex help
• regex pattern for the string
• Question about Javascript Regex
• Regular Expression
• Validating Guid string before passing to Guid constructor
• .net regex quick reference