Hi *, 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..? Cheers, Michael Fox
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
On Fri, 12 Apr 2002, Michael Fox wrote:
Hi *,
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..?
If you want to be really on top of it you could transliterate the example code from "Mastering Regular Expressions" which builds a regular expression for parsing rfc822 email addresses, be warned it's about 60 lines of characters. http://www.oreilly.com/regex is the website for the book, which is perl-oriented but python friendly and very useful if you want to explore the power of the re module. http://www.efn.org/~laprice ( Community, Cooperation, Consensus http://www.opn.org ( Openness to serendipity, make mistakes http://www.efn.org/~laprice/poems ( but learn from them.(carpe fructus ludi)
Bruce Eckels gave me this. It has been working very fine for me. Peter def ShouldBeNone(result): return result is not None def ShouldNotBeNone(result): return result is None tests = ( (re.compile("^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$"), ShouldNotBeNone, "Fail$ (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, "Fai$) def ValidEmailAddress(address, debug=None): for test in tests: if test[1](test[0].search(address)): if debug: return test[2] return 0 return 1 On Friday 12 April 2002 07:19, Michael Fox wrote:
Hi *,
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..?
Cheers,
Michael Fox
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Peter Bengtsson wrote:
Bruce Eckels gave me this. It has been working very fine for me. Peter
def ShouldBeNone(result): return result is not None def ShouldNotBeNone(result): return result is None
tests = ( (re.compile("^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$"), ShouldNotBeNone, "Fail$ (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, "Fai$)
def ValidEmailAddress(address, debug=None): for test in tests: if test[1](test[0].search(address)): if debug: return test[2] return 0 return 1
I do understand that it is possible to create code that checks a lot more than my simple snippet. But my problem with code like that is that it gives an impression of a precission that really isn't there. You can check all you want ind still not be shure it's a valid mail adress. But someone might assume that a mailadress that is checked with such precision is always valid, and so they might skip the nessacary tests to check if mail bounces elsewhere in code. It's like making an estimated time like 35.2313 hours in a budget. Then when you are an hour late the customer complains. Where if you had said 1 week +- a day to make the precision more clear it would be allright. regards Max M, Software Psychologist ;-)
Max M wrote:
It's like making an estimated time like 35.2313 hours in a budget. Then when you are an hour late the customer complains. Where if you had said 1 week +- a day to make the precision more clear it would be allright.
Hey, you work 35 hours a week? Is your company searching for programmers ;-)? cheers, oliver
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..?
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65215
participants (6)
-
Larry Price -
Max M -
Michael Fox -
Oliver Bleutgen -
Peter Bengtsson -
Steve Drees