Julian Turner wrote:
Dag Sunde wrote: function validateEmail(e Mail) {
return /^(\w+\.)*(\w+)@ (\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}
[...]
Hi. A quick search on google threw up a million (well perhaps a
billion) examples of e-mail validation reg-exps.
Here is one I found to tinker with (worked when I tested it):-
var
r=/^([a-zA-Z][\w\.-]*[a-zA-Z0-9])@([a-zA-Z0-9][\w-]*[a-zA-Z0-9])\
([a-zA-Z][a-zA-Z\.]*[a-zA-Z])$/;
I suspect it has limits, such as, needs at least 2 characters in each
part of the e-mail address.
If I'm not mistaken, a Regular Expression for e-mail addresses adhering
closely to "3.4.1. Addr-spec specification" in RFC2822 "Internet Message
Format" would be:
addr-spec
==> local-part "@" domain
==> dot-atom / quoted-string / obs-local-part "@" domain
==> [CFWS] dot-atom-text [CFWS] / quoted-string / obs-local-part "@" domain
(We ignore comments and folding white space.)
==> 1*atext *("." 1*atext) / quoted-string / obs-local-part "@" domain
(We ignore quoted strings and obsolete addressing.)
==> 1*atext *("." 1*atext) "@" domain
==> [!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+(\.[!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+)*
"@" dot-atom / domain-literal / obs-domain
(We ignore obsolete addressing again; and I have yet to see an e-mail
address produced by the domain-literal production, so I ignore this as
well.)
==> [!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+(\.[!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+)*
@
[!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+(\.[!#$%&'*+"/=?^_`{|}~0-9A-Za-z-]+)*
Which leaves us with the fairly simple :)
/^[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+(\
[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+)*@[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+(\
[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+)*/
(Watch for word wrap!)
Several characters in the above range have adjacent code points in
ASCII, so we can compact it a little bit.
/^[!-'*+=?{-~\/-9A-Z^-z-]+(\.[!-'*+=
{-~\/-9A-Z^-z-]+)*@[!-'*+=?^-`{-~\/-9A-Z^-z-]+(\.[!-'*+=
{-~\/-9A-Z^-z-]+)*/
However, since we are on the Internet, I don't think this will suffice.
For example, I don't know a top-level domain with only one character
(I know such a second-level domain, though) and on today's Internet,
the domain-part should be a FQDN (fully qualified domain name):
/^[!-'*+=?{-~\/-9A-Z^-z-]+(\.[!-'*+=
{-~\/-9A-Z^-z-]+)*@[!#$%&'*+"\/=?^_`{|}~0-9A-Za-z-]+\.[!-'*+=
{-~\/-9A-Z^-z-]{2,}/
It could be further refined, allowing only existing top-level domains
and possible second-level domains, allowing especially for IDN domain
names aso., but I think this will suffice for now.
Of course, my tests showed addresses with hyphens are recognized as
correct and no false positives to date (using rather strange examples
and sender addresses of posters here). I'll be happy if you provide
me with one that does not fit :)
HTH
PointedEars