Thursday, October 14, 2004

Email Address Validation in C# using RegEx Pattern

Sometimes there may arise a situation where in we need to validate an email address through code using C#.
This is a sample to do it.
Ex code to do it using the regex libraries.
It's an incredibly complex regular expression to be compliant with RFC822

using System.Text;
using System.Text.RegularExpressions;
using System.Text.RegularExpressions.Regex;

public static bool ValidateEmailPattern(string String)
{
Regex mailPattern = new Regex(@"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25}$");
return (mailPattern .IsMatch(String));
}

the string is the email address, u need to send the string and the function returns true if its good, or false if it does not match.

Check this link for further reading
http://www.thecodeproject.com/csharp/rfc822validator.asp


No comments: