|
|
Comments and Discussions
|
|
 |
|

|
Owesome regex i've ever seen. I've searching for the best email regex, finally i found your's was the best. Thank you so much. My vote 5!
|
|
|
|

|
1. Now registrars allows domain names to start with numbers, so please update your article to allow user@5domain.com
2. Don't you know there's .museum domain? So why you don't add support for it? But I think the only 6 letters domain is .museum, so be careful about user@domain.office which is invalid!
|
|
|
|

|
I liked the post .Please refer my mail for more info
|
|
|
|
|

|
Hi,
Super article. I have a question since i am not familiar with the Regular Expression.
My criteria is, I have to validate a email with the below conditions,
1) The email should accept only alphanumeric
2) The email should allow only @ . _ - special characters
3) @ should not repeat more than once
4) . _ - special characters should not occur continuously more than 2 times ( e.g., a-b-c@d.com is valid whereas a—b@d.com is not valid since hyphen occurs more than 2 times continuously)
5) ._- special characters should not come in first or the last position both in local part or in domain part.
Please guide me with a solution.
|
|
|
|

|
how to limit user not to enter more than 35 chars before @ in email?
Thanks and Good Day,
Bhavtosh
|
|
|
|

|
I love the break out and that you pointed out what to change to allow the new top level domain ".travel"
|
|
|
|

|
After reading all the preceeding comments, I stand by my vote of 5. In my program I need to validate that tens of thousands of email addresses (which should be valid) are (within a reasonable doubt) valid email addresses. Using this code allows me to filter out the couple dozen (0.03%) that are invalid. HTH, -Chris C.
|
|
|
|

|
Due to what I assume was a stray invisible character, I had a problem with the reg ex. I'm reposting the working version (which in the code editor looks just like the non-working version)
Here it is with comments, includes the update to allow 3m as a TLD.
string strRegex =
@"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@" + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\." + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" + @"([a-zA-Z0-9]+[\w-]+\.)+[a-zA-Z]{2,7})$";
HTH,
-Chris C.
|
|
|
|

|
Is there any reason most regular expression samples don't include digits in the domain names. FOr example, Joe.Bloggs@3m.com I believe will fail in your match.
|
|
|
|

|
You mean domains that start with numbers right? I think his regex is ok so long as the domain doesn't start with a number.
I just got asked to fix this in a place where I used it. I looked it up, it turns out that originally domains couldn't start with numbers. This was changed in 1989. Most still don't though and I guess it's common to read the original RFC when writing these things and miss the update one?
Changing
" + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";"
to
" + @"([a-zA-Z0-9]+[\w-]+\.)+[a-zA-Z]{2,4})$";"
seems to have fixed it for me. I don't have much time to stay and debug tonight though.
|
|
|
|
|

|
Thanks buddy.
The sol' helped us a lot.
Please upload any enhancement if you have on this sol.
|
|
|
|

|
I think that this validation its better:
^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$
this is because this regex takes in count special characters.At the other hand, when using your code, an expression like:
fredd%# tech01@azuan.com its perfectly a valid email nevertheless in the real world it doesn't.
|
|
|
|

|
Hi,
first of all the validation RegEx of this article doesn't match the Email fredd%# tech01@azuan.com given by you
But even though it wouldn't match an Email, you could always extend the valiadtion pattern (see the chapter "How perfact you'd like to be?")
Kind regards,
Mykola
|
|
|
|

|
I have just implemented your email validator and it is perfect. Using php I have never found a proper ready made solution. This is great as I must check emails before importing into phpList from Sage Act , this is done using c#.
|
|
|
|

|
I have been working on an application to parse source code and output formatted, color-coded HTML for posting in blogs, etc. When I stepped back and realized the mess of if-else statements I had applied I knew that what I needed was a RegExp. I never dealt with them and this article was only the second or third I read but I have to say it clarified many things (I still had questions from the first article). Thank you, you have opened a new door for me.
|
|
|
|
|

|
I wrote an app a while back which took a different approach. You first do an MX lookup on the domain part of the email address to get the email server address.
Then connect to the remote SMTP server and issue the HELO command, MAIL FROM and then RCPT TO followed by the specified email address.
If all is ok, it'll come back with an OK response which means the address is probably valid.
However, a lot of SMTP servers these days let ALL email through or otherwise spammers will try every combination and get "No such recipient" errors back for addresses which don't exist, so they'll know for sure which are the valid ones.
At least the domain part will be 100% verified. This will be slower than a RegExp, tho, of course.
|
|
|
|

|
wow. this is worse than worse practice.
in your example, you're assuming that every client using your approach has access to an external name server, and access to the remote SMTP server. in a corporate environment, this is never ever ever the case, which renders your example completely useless.
even if the client had access to those services, the outcome would be more than questionably given the facts you mentioned yourself.
|
|
|
|

|
Yes ... Good article, I must say....
Simple and straight forward.
Abhishek Sur
Web Developer
|
|
|
|

|
Thanks for a great article. It is brief and straight to the point.
|
|
|
|

|
Here is a correct expression for IP addesses which range from 0 to 255 in their octets:
(25[0-5]|2[0-4]\d|[01]?\d{1,2})
instead of your expression of
([0-1]?[0-9]{1,2}|2{1}[0-5]{2})
Note 1: your expression doesn't allow these values 206-209/216-219/.../246-249
Note 1: the "2{1}" cpuld be shorted to "2"
|
|
|
|

|
Thanks for the comments. According corrections have been done already.
|
|
|
|

|
At least some of the adresses claimed in the article "which obviously are not to be considered as valid Email addresses", are in fact valid, and should not be rejected. In addition valid addresses with TLD .museum and .travel are rejected. And the author seems to think a domain cannot start with a digit. (ok, I haven't tested)
So how should email-validation work:
1) Never, ever reject a valid address? (but accept some invalid)
2) Never, ever accept an invalid address? (but dismiss some valid)
3) Or be 100% exact? (and huge) (and needs maintenence)
I think 1) is best. Even if the address is syntactically correct does not mean it exists. You only know if the address works (and your mailsystem accepts it), by sending an email, and check if it arrives.
The simplest check I can think of is "has a @ followed by a ." - if that's ok, then send an email, and wait for someone to click a link. Too simple?
|
|
|
|

|
Hi,
in this article I suggested the way how an email validation procedure could look like, but it shouldn't if policies of an information system have rules 1), 2) or 3).
I've provided detailed clarification of the valiadtion pattern in hope to simplify for everybody
the adjustment to the particular needs. So if you'd like to match an email-address with .travel-, .museum- or .imthemostlengthytopleveldomainever just adjust the top-level domain part from "[a-zA-Z]{2,4}" to "[a-zA-Z]{2,4000}"
with best regards,
Mykola
|
|
|
|

|
just what I needed
but I wanted to ask if you can replace the content of the IsEmail function with the following line:
return !string.IsNullOrEmpty(email) && Regex.IsMatch(email, MatchEmailPattern);
I just love the compact C# code.^^
_____________________________
The force of .NET is with me!
|
|
|
|

|
Hi,
of course you can add a pre-validation expression if you'd like to. In the given
code I check whether email-string is null (otherwise IsMatch will throw an exception).
An empty string won't match anyway, thus I haven't to check it actually.
Best regards,
Mykola
|
|
|
|

|
The point was to write a shorter code. Of course you can leave it as email != null
_____________________________
The force of .NET is with me!
|
|
|
|

|
I also keeped the readability in mind, therefore this check condition has become such a flavor.
|
|
|
|
|
|

|
Thnx man ,
it's useful Article .
SirAsad : God Does Not Play dice .
|
|
|
|

|
This is a good first article, and does a good job explaining how the regex patterns are being used but you should add a section on the assumptions that were made concerning the regular expression.
Regex matching for email addresses is notoriously difficult depending on how strictly you want to follow the RFC rules. I have not seen a regex that matches all of the RFC rules properly that is practical to use.
You are correct that many email servers only implement a subset of the RFC for valid addresses, however an address validation component shouldn't consider that if it is to be a "general use" component. That being said, even the MailAddress class in .NET doesn't fully validate against the RFC.
Scott.
—In just two days, tomorrow will be yesterday.
—Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines] [ Articles] [ Blog]
|
|
|
|

|
There is someone[^] who's written an RFC compliant regex (well the most compliant I've come across), it weighs in at a whopping 6.2kB.
I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder
|
|
|
|

|
Ed.Poore wrote: weighs in at a whopping 6.2kB
Yikes! I absolutely would not want to use that. It might valid against the full RFC, but at that size and low (non-existent?) maintanability it doesn't really matter. It is, however, a good proof of the flexibility of regular expressions...but not in a practical manner.
Scott Dorman Microsoft® MVP - Visual C# | MCPD
President - Tampa Bay IASA
Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai
[ Forum Guidelines][ Articles][ Blog]
|
|
|
|

|
Bit like the IOCCC[^], just scarier.
I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder
|
|
|
|

|
but I think you'd then want to extract the domain and do a dns lookup against it, see if it has a mx record etc to further checking - it depends on to what depth you wish to go I guess
'g'
|
|
|
|

|
Yes, I'm absolutely agree with you. The email regex example is given here as an example how the validation expression could look like. How deep you'd like to go and how fine you'll make the validation process depends on your time and budget if you are a professional or on your motivation if you are a hobbyist.
Best regards,
Mykola.
|
|
|
|
|

|
Well, you are right in general.
But even though that according to RFC 2822 the local part of an email address may use following ASCII characters ! # $ % * / ? | ^ { } ` ~ & ' + - = _, some mail-systems are more restrictiv in email addresses created on the mail-system as well as in email addresses to which messages can be sent.
Hotmail, for example, only allows creation of email addresses using alphanumerics and . _ - charachters, and will refuse creating a hotmail mail-account as well as sending mail to any email address containing ! # $ % * / ? | ^ { } ` ~ & ' + = charachters.
And finally, the given email validation regular expression shouldn't be seen as something constant. It can be freely modified to satisfy particular needs.
Best regards,
Mykola
|
|
|
|

|
Mykola Dobrochynskyy wrote: some mail-systems are more restrictiv in email addresses
Probably because they're not using a very good regular expression for validation.
We encourage them to improve themselves, not lower own standards.
|
|
|
|

|
You're right of course. But the smartest regular expression ever (even 100% RFC compatible)
is not very useful, when it fails to validate a common e-mail account.
Best regards,
Mykola
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
This article discusses the topic of validation of an Email address with regular expressions, and finally presents a C# working example project.
| Type | Article |
| Licence | CPOL |
| First Posted | 8 Jan 2008 |
| Views | 238,203 |
| Downloads | 1,497 |
| Bookmarked | 106 times |
|
|