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
For check of email use email_validator
The only really accurate way of distinguishing real, valid email addresses from invalid ones is to send mail to it. What counts as an email is surprisingly convoluted (
"John Doe" <john.doe@example.com>"
actually is a valid email address), and you most likely want the email address to actually send mail to it later. After it passes some basic sanity checks (such as in Thomas's answer, has an@
and at least one.
after the@
), you should probably just send an email verification letter to the address, and wait for the user to follow a link embedded in the message to confirm that the email was valid.Email addresses are incredibly complicated. Here's a sample regex that will match every RFC822-valid address: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
You'll notice that it's probably longer than the rest of your program. There are even whole modules for Perl with the purpose of validating email addresses. So you probably won't get anything that's 100% perfect as a regex while also being readable. Here's a sample recursive descent parser: http://cpansearch.perl.org/src/ABIGAIL/RFC-RFC822-Address-2009110702/lib/RFC/RFC822/Address.pm
but you'll need to decide whether you need perfect parsing or simple code.
Abovementioned parseaddr would ignore the trailing @.
Probably extract address and compare to the original?
Has anybody tried validate.email ?
I haven't seen the answer already here among the mess of custom Regex answers, but...
Python has a module called validate_email which has 3 levels of email validation, including asking a valid SMTP server if the email address is valid (without sending an email).
Check email string is valid format:
Check if the host has SMTP Server:
Check if the host has SMTP Server and the email really exists:
For those interested in the dirty details, validate_email.py (source) aims to be faithful to RFC 2822.
To install with pip
and you'll need the pyDNS module for checking SMTP servers
or from Ubuntu