email syntax validation needed

Nevin Pratt nevin at smalltalkpro.com
Sat Jul 5 05:45:46 UTC 2003



Derek Brans wrote:

> Does anyone have code (or is there any in the image) which, given a 
> string, returns whether or not that string is a syntactically valid 
> email address?
>  
> Thanks.
>  
> Derek Brans
> Nerd on a Wire
> Web design that's anything but square
> http://www.nerdonawire.com
> mailto: brans at nerdonawire.com <mailto:brans at nerdonawire.com>
> phone: 604.874.6463
> toll-free: 1-877-NERD-ON-A-WIRE
>
>------------------------------------------------------------------------
>
>
>  
>

Derek,

What did you finally use in your quest on this topic?  Here is a piece 
of code I use.  It requires that the email address have a $@ character 
in it.  It requires that the hostname portion following that character 
be resolvable via DNS (and it must resolve within 5 seconds).  And it 
requires that the username portion of it not have a space (I've found 
putting spaces in the username to be a very common user error).  You can 
see I can easily expand the list of bad characters in the username 
portion if I choose to, although I never have:

isValidEmailAddress: emailAddress
    | username |
    emailAddress isNil
        ifTrue: [^ false].
    (NetNameResolver
            addressForName: (emailAddress copyAfter: $@)
            timeout: 5) isNil
        ifTrue: [^ false].
    username _ emailAddress copyUpTo: $@.
    (username includesAnyOf: ' ')
        ifTrue: [^ false].
    ^ true

-- 
Nevin Pratt
Bountiful Baby
http://www.bountifulbaby.com
(801) 992-3137





More information about the Squeak-dev mailing list