Regex Email validation
I have an expression for checking email addresses that I use.
Since none of the above were as short or as accurate as mine, I thought I would post it here.
@"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*"
+ "@"
+ @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$";
For more info go read about it here: C# – Email Regular Expression
Also, this checks for RFC validity based on email syntax, not for whether the email really exists. The only way to test that an email really exists is to send and email and have the user verify they received the email by clicking a link or entering a token.
Then there are throw-away domains, such as Mailinator.com, and such. This doesn't do anything to verify whether an email is from a throwaway domain or not.
TLD's like .museum aren't matched this way, and there are a few other long TLD's. Also, you can validate email addresses using the MailAddress class as Microsoft explains here in a note:
Instead of using a regular expression to validate an email address, you can use the System.Net.Mail.MailAddress class. To determine whether an email address is valid, pass the email address to the MailAddress.MailAddress(String) class constructor.
public bool IsValid(string emailaddress)
{
try
{
MailAddress m = new MailAddress(emailaddress);
return true;
}
catch (FormatException)
{
return false;
}
}
This saves you a lot af headaches because you don't have to write (or try to understand someone else's) regex.
EDIT: For those who are allergic to try/catch: In .NET 5 you can use MailAddress.TryCreate
. See also https://stackoverflow.com/a/68198658, including an example how to fix .., spaces, missing .TLD, etc.
I found nice document on MSDN for it.
How to: Verify that Strings Are in Valid Email Format http://msdn.microsoft.com/en-us/library/01escwtf.aspx (check out that this code also supports the use of non-ASCII characters for Internet domain names.)
There are 2 implementation, for .Net 2.0/3.0 and for .Net 3.5 and higher.
the 2.0/3.0 version is:
bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
My tests over this method give:
Invalid: @majjf.com
Invalid: A@b@[email protected]
Invalid: Abc.example.com
Valid: [email protected]
Valid: [email protected]
Invalid: js*@proseware.com
Invalid: [email protected]
Valid: [email protected]
Valid: [email protected]
Invalid: ma@@jjf.com
Invalid: ma@jjf.
Invalid: [email protected]
Invalid: [email protected]
Invalid: ma_@jjf
Invalid: ma_@jjf.
Valid: [email protected]
Invalid: -------
Valid: [email protected]
Valid: [email protected]
Valid: [email protected]
Valid: [email protected]
Invalid: [email protected]
Valid: j_9@[129.126.118.1]
Valid: [email protected]
Invalid: js#[email protected]
Invalid: [email protected]
Invalid: [email protected]
Valid: [email protected]
Valid: [email protected]
Valid: [email protected]
Valid: [email protected]
Invalid: [email protected]
Invalid: [email protected]
Valid: [email protected]
Valid: [email protected]
Valid: [email protected]
Valid: [email protected]
Valid: [email protected]
Valid: [email protected]
Valid: [email protected]
I think @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"
should work.
You need to write it like
string email = txtemail.Text;
Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
Match match = regex.Match(email);
if (match.Success)
Response.Write(email + " is correct");
else
Response.Write(email + " is incorrect");
Be warned that this will fail if:
There is a subdomain after the
@
symbol.You use a TLD with a length greater than 3, such as
.info