If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Perhaps. The regex I supplied has never let me down yet. We must live in the real world, not academia.

Yours would fail for any travel agents or museum staff. It would also fail for anyone with valid but unusual separators in the name part of their address - for example if ! is used as the separator between the first and last names.

The following is a perfectly valid and reasonably likely to be used email address that would fail your validation for two reasons.

joe!bloggs@somewhere.travel

If you just want a generic email validation then /^.{1,255}@((\w)([\w\-]?)+\.)+([a-z]{2,6})$/ would be a better choice - the final validation should be done on the server anyway (which will use the regexp I posted earlier) so something more generic that isn't going to flag too many valid addresses as invalid is appropriate. Just about anything is allowed in front of the @ provided it is enclosed in "" so simly testing that it isn't too long is more appropriate than deciding that 99% of what is allowed will be considered to be invalid.

\Anyway how do you know your regexp has never let you down - those whose valid email addresses you reject probably just went elsewhere and ignore your site.

Yours would fail for any travel agents or museum staff. It would also fail for anyone with valid but unusual separators in the name part of their address - for example if ! is used as the separator between the first and last names.

The following is a perfectly valid and reasonably likely to be used email address that would fail your validation for two reasons.

joe!bloggs@somewhere.travel

\Anyway how do you know your regexp has never let you down - those whose valid email addresses you reject probably just went elsewhere and ignore your site.

We have discused this before - I have never ever encounted a travel or museum domain in the real world. And you have never been able to point to an actual example. But if you really want to cater for that then change {2,4} to {2,6}. I think that if anything that adds to the risk of passing an invalid address. And what about the French barreau domain for barristers (lawyers)?

The real and principal risk with entering email addresses is simple mistyping which no regex (even your monstrous example) can detect. That is why it is best to get the user to enter his address twice, and then ensure that the two match. In that case testing with a regex may be superfluous - if the user cannot get his own email address right then it does not matter whether it is valid in the technical sense or not. And mistyping joebloggs@somewhere.com as joe!bloggs@somewhere.com is caught by my test!
Note that the address joe!bloggs@somewhere.com is not recognised as an email and placed in email tags by this forum!

In 15 years no-one has ever informed me that they (or a customer) had a valid email address which was not accepted by my regex. I accept that such may exist. I think that you would encounter one as often as a nun in a bikini. "Funny" or weird email addresses are used only by spammers and criminals. By contrast it is not uncommon to find that a user has entered his address mis-spelled. btinertnet is quite common - here the mistake is obvious.

Last edited by Philip M; 05-08-2012 at 10:09 AM.
Reason: Noticed typo

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.