Validating email addresses using System.Text.RegularExpressions

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

I'm a little confused by this functionality. It doesn't seem to be
behaving like it should.

I am using the following regular expression to validate email
addresses:
"\w+([-+.]\w+)*@\w+([-.]\w+)*\.([a-zA-Z]{2,4})\040*". From what I can
determine it should validate the following rules:

1. BEFORE THE AMPERSAND
A. Must contain at least one alphanumeric character.
B. Can contain a '-', '+', or '.' character but if it does it
must have a alphanumeric character on either side of it.
2. AFTER THE AMPERSAND BUT BEFORE THE '.'
A. Must contain at least one alphanumeric character.
B. Can contain a '-', or '.' character but if it does it must
have a alphanumeric character on either side of it.
3. AFTER THE '.'
A. Must contain at least two alphabetical characters but no more
than 4.

However when I use System.Text.RegularExpressions.RegEx with this
expression and use the IsMatch method and use an email address like
(e-mail address removed)@[email protected] it returns true as if that was
a valid email address based on the rules.

Doesn't the {2,4} mean that it has to have a minimum of 2 characters
after the '.' character but no more than 4? Also, doesn't the
[a-zA-Z] mean that the characters after the '.' must be alphabetical
only? My tests seem to prove otherwise.

Any help on this would be great...thanks!
 
Hi Doug,

Here is the example in MSDN about validating email addresses

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})(\]?)$");
}
 
(e-mail address removed) (Doug) wrote in
I'm a little confused by this functionality. It doesn't seem to
be behaving like it should.

I am using the following regular expression to validate email
addresses:
"\w+([-+.]\w+)*@\w+([-.]\w+)*\.([a-zA-Z]{2,4})\040*". From what
I can determine it should validate the following rules:

1. BEFORE THE AMPERSAND
A. Must contain at least one alphanumeric character.
B. Can contain a '-', '+', or '.' character but if it does
it
must have a alphanumeric character on either side of it.
2. AFTER THE AMPERSAND BUT BEFORE THE '.'
A. Must contain at least one alphanumeric character.
B. Can contain a '-', or '.' character but if it does it
must
have a alphanumeric character on either side of it.
3. AFTER THE '.'
A. Must contain at least two alphabetical characters but no
more
than 4.

However when I use System.Text.RegularExpressions.RegEx with
this expression and use the IsMatch method and use an email
address like (e-mail address removed)@[email protected] it returns
true as if that was a valid email address based on the rules.

Doesn't the {2,4} mean that it has to have a minimum of 2
characters after the '.' character but no more than 4? Also,
doesn't the [a-zA-Z] mean that the characters after the '.' must
be alphabetical only? My tests seem to prove otherwise.

Any help on this would be great...thanks!

Here's a method I use to validate e-mail addresses:

public static bool IsValidEmailAddress(string email)
{
if ((email != null) && (email.Trim().Length > 0))
{
return Regex.IsMatch(email, @"
^
[-a-zA-Z0-9][-.a-zA-Z0-9]*
@
[-.a-zA-Z0-9]+
(\.[-.a-zA-Z0-9]+)*
\.
(
com|edu|info|gov|int|mil|net|org|biz|
name|museum|coop|aero|pro
|
[a-zA-Z]{2}
)
$",
RegexOptions.IgnorePatternWhitespace);
}
else
{
return false;
}
}


You can find other regexes to validate e-mail addresses here:

http://regexlib.com/
 
Back
Top