How to check for valid email address?

2018-12-31 20:32发布

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.

18条回答
路过你的时光
2楼-- · 2018-12-31 20:51

For check of email use email_validator

from email_validator import validate_email, EmailNotValidError

def check_email(email):
    try:
        v = validate_email(email)  # validate and get info
        email = v["email"]  # replace with normalized form
        print("True")
    except EmailNotValidError as e:
        # email is not valid, exception message is human-readable
        print(str(e))

check_email("test@gmailcom")
查看更多
步步皆殇っ
3楼-- · 2018-12-31 20:53

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.

查看更多
梦寄多情
4楼-- · 2018-12-31 20:55

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.

查看更多
有味是清欢
5楼-- · 2018-12-31 20:55

Abovementioned parseaddr would ignore the trailing @.

from email.utils import parseaddr
parseaddr('aaa@bbb@ccc.com') ('', 'aaa@bbb')

Probably extract address and compare to the original?

Has anybody tried validate.email ?

查看更多
路过你的时光
6楼-- · 2018-12-31 20:58

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:

from validate_email import validate_email
is_valid = validate_email('example@example.com')

Check if the host has SMTP Server:

is_valid = validate_email('example@example.com',check_mx=True)

Check if the host has SMTP Server and the email really exists:

is_valid = validate_email('example@example.com',verify=True)

For those interested in the dirty details, validate_email.py (source) aims to be faithful to RFC 2822.

All we are really doing is comparing the input string to one gigantic regular expression. But building that regexp, and ensuring its correctness, is made much easier by assembling it from the "tokens" defined by the RFC. Each of these tokens is tested in the accompanying unit test file.


To install with pip

pip install validate_email

and you'll need the pyDNS module for checking SMTP servers

pip install pyDNS

or from Ubuntu

apt-get python3-dns
查看更多
冷夜・残月
7楼-- · 2018-12-31 20:58
"^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$"
查看更多
登录 后发表回答