Is there a good way to check a form input using regex to make sure it is a proper style email address? Been searching since last night and everybody that has answered peoples questions regarding this topic also seems to have problems with it if it is a subdomained email address.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
See validate_email docs.
Email addresses are not as simple as they seem! For example, Bob_O'Reilly+tag@example.com, is a valid email address.
I've had some luck with the lepl package (http://www.acooke.org/lepl/). It can validate email addresses as indicated in RFC 3696: http://www.faqs.org/rfcs/rfc3696.html
Found some old code:
I found an excellent (and tested) way to check for valid email address. I paste my code here:
If you want to take out the mail from a long string or file Then try this.
Note, this will work when you have a space before and after your email-address. if you don't have space or have some special chars then you may try modifying it.
Working example:
This will take out example@me.com from this string.
Also, note this may not be the right answer.. But I have posted it here to help someone who have specific requirement like me
email validation
The Python standard library comes with an e-mail parsing function:
email.utils.parseaddr()
.It returns a two-tuple containing the real name and the actual address parts of the e-mail:
And if the parsing is unsuccessful, it returns a two-tuple of empty strings:
An issue with this parser is that it's accepting of anything that is considered as a valid e-mail address for RFC-822 and friends, including many things that are clearly not addressable on the wide Internet:
So, as @TokenMacGuy put it, the only definitive way of checking an e-mail address is to send an e-mail to the expected address and wait for the user to act on the information inside the message.
However, you might want to check for, at least, the presence of an @-sign on the second tuple element, as @bvukelic suggests:
If you want to go a step further, you can install the dnspython project and resolve the mail servers for the e-mail domain (the part after the '@'), only trying to send an e-mail if there are actual
MX
servers:You can catch both
NoAnswer
andNXDOMAIN
by catchingdns.exception.DNSException
.And Yes,
foo@bar@google.com
is a syntactically valid address. Only the last@
should be considered for detecting where the domain part starts.