Actually there is no simple regular expression for this problem. Confirmation email tokens are the only correct way to know you got the address of the person entering it. This is why most mailing lists now use that mechanism to confirm sign-ups. After all, anybody can put down
president@whitehouse.gov
, and that will even parse as legal, but it isn't likely to be the person at the other end.
This regular expression will only validate addresses that have had any comments stripped and replaced with whitespace (this is done by the module).
If you're looking for something simpler but that will catch most valid email addresses try something like:
"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
Remember, After all, the best way to validate the email address is still to actually send an email to the address in question to validate the address. If the email address is part of user authentication (register/login/etc), then you can perfectly combine it with the user activation system. I.e. send an email with a link with an unique activation key to the specified email address and only allow login when the user has activated the newly created account using the link in the email.
If the purpose of the regex is just to quickly inform the user in the UI that the specified email address doesn't look like in the right format, best is still to check if it matches basically the following regex:
^([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)$
Not to mention that non-Latin (Chinese, Arabic, Greek, Hebrew, Cyrillic and so on) domain names are to be allowed in the near future. Everyone has to change the email regex used, because those characters are surely not to be covered by [a-z]/i
nor \w
. They will all fail.
Tags:
php regexp
→
validate
What's the best regular expression you have or have seen for validating emails?
By Game Changer →
Saturday, December 5, 2015