是否有一个好方法来检查表单输入使用正则表达式,以确保它是一个正确的风格的电子邮件地址?从昨晚开始搜索,每个人都回答了关于这个话题的问题,如果它是一个子域名的电子邮件地址,似乎也有问题。


当前回答

在电子邮件输入上使用此过滤器掩码: emailMask: / (\ w .\-@'"!#$%&'*+/=?^ _{|} ~) /我

其他回答

电子邮件地址非常复杂。下面是一个匹配每个rfc822有效地址的示例正则表达式: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html

您会注意到它可能比程序的其他部分要长。甚至还有用于验证电子邮件地址的Perl完整模块。所以你可能不会得到任何东西,100%完美的正则表达式,同时也可读。下面是一个递归下降解析器示例: http://cpansearch.perl.org/src/ABIGAIL/RFC-RFC822-Address-2009110702/lib/RFC/RFC822/Address.pm

但是您需要决定是需要完美的解析还是简单的代码。

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.

"^[\w\.\+\-]+\@[\w]+\.[a-z]{2,3}$"
from validate_email import validate_email
is_valid = validate_email('example@example.com',verify=True)
print(bool(is_valid))

参见validate_email文档。

这通常是使用正则表达式解决的。然而,有许多不同的解决方案。这取决于您需要的严格程度,以及您是否有自定义的验证要求,或者是否接受任何有效的电子邮件地址。

参考页面:http://www.regular-expressions.info/email.html