[Zope] Email address validator?

Bruce Eckel Bruce@EckelObjects.com
Sat, 24 Nov 2001 20:10:40 -0800


Thanks! I spent a bit of time refactoring it. Also, I decided that if it=
 worked it would return the address, otherwise it would return None to=
 indicate failure.

import re

def ShouldBeNone(result): return result is not None
def ShouldNotBeNone(result): return result is None

tests =3D (
  (re.compile("^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$"), ShouldNotBeNone,=
 "Failed a"),
  (re.compile("^[^0-9a-zA-Z]|[^0-9a-zA-Z]$"), ShouldBeNone, "Failed b"),
  (re.compile("([0-9a-zA-Z]{1})\@."), ShouldNotBeNone, "Failed c"),
  (re.compile(".\@([0-9a-zA-Z]{1})"), ShouldNotBeNone, "Failed d"),
  (re.compile(".\.\-.|.\-\..|.\.\..|.\-\-."), ShouldBeNone, "Failed e"),
  (re.compile(".\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_."), ShouldBeNone, "Failed=
 f"),
  (re.compile(".\.([a-zA-Z]{2,3})$|.\.([a-zA-Z]{2,4})$"), ShouldNotBeNone,=
 "Failed g"),
)

def valid_email(address, debug=3DNone):
  for test in tests:
    if test[1](test[0].search(address)):
      if debug: return test[2]
      return None
  return address

def tryAddress(address):
  print address, valid_email(address, debug=3D1)

if __name__ =3D=3D '__main__':
  #test a few:
  tryAddress('z+z.e@bun.org')
  tryAddress('z.e@.org')
  tryAddress('_az.e@bun.org')
  tryAddress('e@b.g')
  tryAddress('a_-.e@bun.org')
  tryAddress('ok.computer@-bun.org')
  tryAddress('ok.e@bun.org')
  tryAddress('OK@bun.org')
  tryAddress('t@bun.info')
  tryAddress('my friend@bun.org')
  tryAddress('my.friend.bun.org')



*********** REPLY SEPARATOR  ***********

On 11/24/01 at 1:12 PM Jim Washington wrote:

>Barry A. Warsaw wrote:
>
>>>>>>>"BE" =3D=3D Bruce Eckel <Bruce@EckelObjects.com> writes:
>>>>>>>
>>
>>    BE> Worked like a charm! It turns out I was handing it an invalid
>>    BE> "from" email address.
>>
>>    BE> Which brings up another question -- is there a piece of code
>>    BE> that will validate email addresses somewhere? Seems like it
>>    BE> would be a common tool (I'll bet the spammers have it!).
>>
>>Do you mean you want to find out if a string looks like a valid email
>>address, or that it actually successfully delivers to some end
>>destination?  The former is doable, the latter is exceedingly
>>difficult!
>>
>>Mailman has the following bit of code, which accomplishes the goal set
>>out in its docstring <wink>:
>>
>I hadn't thought of looking through mailman.  Here's one I put together 
>a couple of weeks ago that seems a bit more thorough...  
>
>-- Jim Washington
>
>
>
># This is hacked from an existing script written in perl, licensed "public
>domain"
># The original perl script: 
>http://aspn.activestate.com/ASPN/Cookbook/Rx/Recipe/68432 2Nov2001
># author e-mail: tbyte@bolehmail.com
>
>"""
>Text of the perl script:
>
>sub ValidEmailAddr { #check if e-mail address format is valid
>
>my $mail =3D shift;                                                  #in
>form name@host
>
>return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ );
>#characters allowed on name: 0-9a-Z-._ on host: 0-9a-Z-. on between: @
>return 0 if ( $mail =3D~ /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/);             #must
>start or end with alpha or num
>return 0 if ( $mail !~ /([0-9a-zA-Z]{1})\@./ );                    #name
>must end with alpha or num
>return 0 if ( $mail !~ /.\@([0-9a-zA-Z]{1})/ );                    #host
>must start with alpha or num
>return 0 if ( $mail =3D~ /.\.\-.|.\-\..|.\.\..|.\-\-./g );           #pair
>.- or -. or -- or .. not allowed
>return 0 if ( $mail =3D~ /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./g );    #pair
>._ or -_ or _. or _- or __ not allowed
>return 0 if ( $mail !~ /\.([a-zA-Z]{2,3})$/ );                     #host
>must end with '.' plus 2 or 3 alpha for TopLevelDomain (MUST be modified
>in future!)
>
>return 1;
>}
>
>e-mail checker, checks whether an e-mail address is formatted properly.
>usage:
>
>from email_checker import valid_email
>
>s =3D "string to be tested"
>if valid_email(s):
>  do_something()
>else:
>  do something_else()
>
>"""
>
>import re
>
>rega =3D re.compile("^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$") #failure a
>regb =3D re.compile("^[^0-9a-zA-Z]|[^0-9a-zA-Z]$")            #failure b
>regc =3D re.compile("([0-9a-zA-Z]{1})\@.")                    #failure c
>regd =3D re.compile(".\@([0-9a-zA-Z]{1})")                    #failure d
>rege =3D re.compile(".\.\-.|.\-\..|.\.\..|.\-\-.")            #failure e
>regf =3D re.compile(".\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_.")     #failure f
>regg =3D re.compile(".\.([a-zA-Z]{2,3})$|.\.([a-zA-Z]{2,4})$")#failure g
>
>def valid_email(address, debug=3DNone):
>    fail =3D None
>    if rega.search(address) is None:
>        fail =3D 'a'
>    elif regb.search(address) is not None:
>        fail =3D 'b'
>    elif regc.search(address) is None:
>        fail =3D 'c'
>    elif regd.search(address) is None:
>        fail =3D 'd'
>    elif rege.search(address) is not None:
>        fail =3D 'e'
>    elif regf.search(address) is not None:
>        fail =3D 'f'
>    elif regg.search(address) is None:
>        fail =3D 'g'
>    if fail is not None:
>        if debug:
>            return address,fail,None
>        else:
>            return None
>    if debug:
>        return address,1,1
>    else:
>        return 1
>
>if __name__ =3D=3D '__main__':
>  #test a few:
>  print valid_email('z+z.e@bun.org', debug=3D1)
>  print valid_email('z.e@.org', debug=3D1)
>  print valid_email('_az.e@bun.org', debug=3D1)
>  print valid_email('e@b.g', debug=3D1)
>  print valid_email('a_-.e@bun.org', debug=3D1)
>  print valid_email('ok.computer@-bun.org', debug=3D1)
>  print valid_email('ok.e@bun.org', debug=3D1)
>  print valid_email('OK@bun.org', debug=3D1)
>  print valid_email('t@bun.info', debug=3D1)
>  print valid_email('my friend@bun.org', debug=3D1)
>  print valid_email('my.friend.bun.org', debug=3D1)



Most current information can be found at:
http://www.mindview.net/Etc/notes.html
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Bruce Eckel    http://www.BruceEckel.com
Contains free electronic books: "Thinking in Java 2e" & "Thinking in C++=
 2e"
Please subscribe to my free newsletter -- just send any email to:
join-eckel-oo-programming@earth.lyris.net
My schedule can be found at:
http://www.mindview.net/Calendar
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D