Email address check function

Georg Mischler schorsch at schorsch.com
Fri Dec 3 07:53:30 EST 1999


Michael Stroeder wrote:
> Gerrit Holl wrote:
> >
> > I'm writing some CGI scripts and I want the user to fill in their
> > real  email address. Checking this is more difficult than just
> > look if it  contains an '@'.
> > There must be at least one '.' after the '@' but there must be
> > non-'@'  chars before and after every '.', no white space, etc.
>
> How about this:
> re.compile(r"^([\w at .=/_-]+)@([\w-]+)(\.[\w-]+)*$").match(emailaddress)
>
> Well, I think there are some real regex gurus here which can
> improve my weak guess and provide even stricter patterns.
>
> > There must be an RFC for this
>
> RFC 822?

Partly. For the right hand side of the @ you might want to
check rfc1035 (DNS) as well:

- There must be at least one "."
- There must be a "name" on each side of the each "." (obviously...)
- "Names" are case insensitive (fold to lowercase internally).
- Every "name" must be at least two, and at most 32 characters long.
- The last (toplevel) "name" must be two or three characters long.
- Every "name" must start with one of [a-z].
- Every "name" may only contain [-a-z0-9] (many nameservers also
  allow the "_", so you may want to accept that as well).

This seems to cover the most common formal errors in SMTP based
mail addresses. Note that this right hand side of the address
*dosen't* necessarily describe a physical host on the internet.
The DNS system may give you any number of hosts when you
request the MX record by this name, all of which may have
arbitrary other names, if any.

You basically can't make any assumptions on the left hand
side of the "@", since that is in the hands of the possibly
proprietary mail system processing the mail on arrival at
the target host.


> You could also check if A or MX records exist for the domain part
> of the email address in the DNS. Watch out for dnslib.py in your
> Python 1.5.2 distr. under demos(?).

If you don't mind your visitor to wait up to three minutes or
five minutes for the result page, go ahead. :)


> You could send some access code via email for further access to
> your system.

This is what most decent mailing list software does and is
highly reccomended if you care for collecting operative mail
addresses. You need to consider if your application is worth
the extra effort required by the user though.


Have fun!

-schorsch

--
Georg Mischler  --  simulation developper  --  schorsch at schorsch.com
+schorsch.com+  --  lighting design tools  --  http://www.schorsch.com/


Sent via Deja.com http://www.deja.com/
Before you buy.




More information about the Python-list mailing list