473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Validating email addresses using System.Text.RegularExpressions

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
dn******@dtgnet.com@dn******@dtgnet.com 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!
Nov 16 '05 #1
2 6602
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})(\]?)$");
}
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Doug" <dn******@dtgnet.com> wrote in message
news:ff**************************@posting.google.c om...
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
dn******@dtgnet.com@dn******@dtgnet.com 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!

Nov 16 '05 #2
dn******@dtgnet.com (Doug) wrote in
news:ff**************************@posting.google.c om:
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 dn******@dtgnet.com@dn******@dtgnet.com 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/

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jeff Griffin | last post by:
I am looking for a good referance on how to validate a string in C#. Specifically have a string which will contain a postal code that may or may not be in the US. I need to check this string to...
8
by: Richard Lionheart | last post by:
Hi All, I tried using RegEx, but the compiler barfed with "The type of namespace 'RegEx' could not be found. Prior to this, I had the same problem with MatchCollection, but discovered it's...
0
by: aj | last post by:
Jordan you can use regular expression (Namespace: System.Text.RegularExpressions) for validating the IP address in the textbox. Regular Expression to validate Web address:...
2
by: larry | last post by:
Hi, I need to use "regex" to validate user input into a text box. I'm having trouble adding the 'regex' class to my project. Essentially, I haven't learned how to add it. can someone tell me...
12
by: Tim Frawley | last post by:
I have a method for testing variable length strings for all digits: Dim ch As String Dim i As Integer AllDigits = True For i = 1 To Len(txt) ' See if the next character is a non-digit. ch =...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
4
by: John | last post by:
Hi I have this problem that client users enter emails incorrectly due to type and either the domain name is invalid (Microsot.com instead of Microsoft.com) or the syntax of the email...
3
by: anu b | last post by:
Hii I am using System.net.mail for sending email... i need to send a webpage with its html content as my email body .....for that i used mail.Isbodyhtml as true...but it is not working for me...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.