[Zope] Valid email address...

Max M maxm@mxm.dk
Fri, 12 Apr 2002 08:58:34 +0200


Michael Fox wrote:

>Not being a python guru myself, I was wondering if anyone could provide a code snippet that checks the value of a dtml-var to see if it's a valid email address..?
>

There is no way to do it correctly without sending an email and see if 
you get an reply, so don't sweat to much over it.

I usually just do something like:

def isValidMail(adress):
    return '@' in adress and '.' in adress

or if you are a bit more paranoid:

def isValidMail(adress):
    if '@' in adress and '.' in adress:
        id, url = adress.split('@')
        domain, topDomain = url.split('.')
        return len(id)>0 and len(domain)>0 and len(topDomain)>0
    return 0

In practice you cannot really do much better even though you tried much 
harder.

you could then call it like:

<dtml-if "isValidMail(adress)">
do stuff
</dtml-if>

regards Max M