Don't use regular expressions to "validate" email addresses (was: Ineed some help with a regexp please)

bruce bedouglas at earthlink.net
Thu Sep 21 21:24:12 EDT 2006


so ben...

if you were creating a web app with an email form... rather than try to
check if the email is valid... you'd create something to allow anyone to
potentially spam the hell out of a system...

my two cents worth... try to verify/validate that the email is valid, and
possibly belongs to the user...

peace...



-----Original Message-----
From: python-list-bounces+bedouglas=earthlink.net at python.org
[mailto:python-list-bounces+bedouglas=earthlink.net at python.org]On Behalf
Of Ben Finney
Sent: Thursday, September 21, 2006 6:07 PM
To: python-list at python.org
Subject: Don't use regular expressions to "validate" email addresses
(was: Ineed some help with a regexp please)


"John Machin" <sjmachin at lexicon.net> writes:

> A little more is unfortunately not enough. The best advice you got was
> to use an existing e-mail address validator. The definition of a valid
> e-mail address is complicated. You may care to check out "Mastering
> Regular Expressions" by Jeffery Friedl. In the first edition, at least
> (I haven't looked at the 2nd), he works through assembling a 4700+ byte
> regex for validating e-mail addresses. Yes, that's 4KB.  It's the best
> advertisement for *not* using regexes for a task like that that I've
> ever seen.

The best advice I've seen when people ask "How do I validate whether
an email address is valid?" was "Try sending mail to it".

It's both Pythonic, and truly the best way. If you actually want to
confirm, don't try to validate it statically; *use* the email address,
and check the result.  Send an email to that address, and don't use it
any further unless you get a reply saying "yes, this is the right
address to use" from the recipient.

The sending system's mail transport agent, not regular expressions,
determines which part is the domain to send the mail to.

The domain name system, not regular expressions, determines what
domains are valid, and what host should receive mail for that domain.

Most especially, the receiving mail system, not regular expressions,
determines what local-parts are valid.

--
 \       "I believe in making the world safe for our children, but not |
  `\    our children's children, because I don't think children should |
_o__)                                  be having sex."  -- Jack Handey |
Ben Finney

--
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list