电子邮件地址验证的最佳实践(包括 gmail 地址中的 +)

发布于 2024-08-14 06:26:33 字数 861 浏览 4 评论 0 原文

我知道这里有很多关于电子邮件验证和特定正则表达式的问题。我想知道具有 [电子邮件受保护] 技巧 (详细信息此处)。我当前用于 JavaScript 验证的 RegExp 如下,但它不支持句柄中额外的 +

/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/

是否还有其他服务支持额外的 +?我应该在地址中允许使用 + 还是应该更改正则表达式以仅允许使用 gmail.comgooglemail.com 的电子邮件作为域名?如果是这样,修改后的正则表达式会是什么?

更新: 感谢大家指出 + 根据规范是有效的。我以前不知道这一点,现在为了未来而这样做。对于那些说使用正则表达式来验证它是不好的人来说,我的理由完全基于我正在构建的创意设计。我们客户的设计在输入的电子邮件地址旁边放置了一个绿色的勾号或一个红色的X,使其模糊。该图标表明它是否是一个有效的电子邮件地址,所以我必须使用一些 JS 来验证它。

I know there are a lot of questions on here about email validation and specific RegEx's. I'd like to know what the best practices are for validating emails with respect to having the [email protected] trick (details here). My current RegExp for JavaScript validation is as follows, but it doesn't support the extra + in the handle:

/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/

Are there any other services that support the extra +? Should I allow a + in the address or should I alter the RegEx to only allow it for an email with gmail.com or googlemail.com as the domain? If so, what would be the altered RegEx?

UPDATE:
Thanks to everyone for pointing out that + is valid per the spec. I didn't know that and now do for the future. For those of you saying that its bad to even use a RegEx to validate it, my reason is completely based on a creative design I'm building to. Our client's design places a green check or a red X next to the email address input on blur of it. That icon indicates whether or not its a valid email address so I must use some JS to validate it then.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

壹場煙雨 2024-08-21 06:26:33

+ 是电子邮件地址中的有效字符。如果域名不是 gmail.com 或 googlemail.com 也没关系,

正则表达式实际上并不是验证电子邮件的好方法,但如果您只想修改正则表达式来处理加号,请将其更改为以下内容:

/^([a-zA-Z0-9_.-\+])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/

作为此正则表达式如何不验证规范的示例:电子邮件 [电子邮件受保护] 根据它有效。

+ is a valid character in an email address. It doesn't matter if the domain isn't gmail.com or googlemail.com

Regexes aren't actually a very good way of validating emails, but if you just want to modify your regex to handle the plus, change it to the following:

/^([a-zA-Z0-9_.-\+])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/

As an example of how this regex doesn't validate against the spec: The email [email protected] is valid according to it.

强辩 2024-08-21 06:26:33

如果您需要通过正则表达式验证电子邮件,请阅读标准 或至少这篇文章

标准建议使用这个正则表达式:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

如果这没有吓到你,它应该:)

If you need to validate emails via regexp, then read the standard or at least this article.

The standard suggests to use this regexp:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

If that doesn't scare you, it should :)

小霸王臭丫头 2024-08-21 06:26:33

我倾向于使用 /.+@.+\..+/ 之类的东西来检查简单的错误。然后我会向该地址发送一封电子邮件以验证它是否确实存在,因为大多数拼写错误仍然会导致语法上有效的电子邮件地址。

I would tend to go with something along the lines of /.+@.+\..+/ to check for simple mistakes. Then I would send an email to the address to verify that it actually exists, since most typos will still result in syntactically valid email addresses.

无力看清 2024-08-21 06:26:33

该规范允许使用一些非常疯狂且丑陋的电子邮件地址。我经常对网站感到非常恼火,甚至抱怨完全正常、有效的电子邮件地址,所以请尽量不要拒绝有效的电子邮件地址。接受一些非法地址比拒绝合法地址要好。

就像其他人建议的那样,我会使用简单的正则表达式,例如 /.+@.+/,然后发送验证电子邮件。如果它足够重要,需要验证,那么它就足够重要,因为合法的电子邮件地址仍然可能属于您的访问者以外的其他人。或者包含无意但致命的拼写错误。

*编辑:从正则表达式的域部分删除了点,因为 a@to 仍然是有效的电子邮件地址。因此,即使我的超级简化验证也拒绝了有效地址。只接受包含 @ 且其前后带有某些内容的所有内容有什么缺点吗?

The specs allow for some really crazy ugly email addresses. I'm often very annoyed by websites even complaining about perfectly normal, valid email addresses, so please, try not to reject valid email addresses. It's better to accept some illegal addresses than to reject legal ones.

Like others have suggested, I'd go with using a simple regexp like /.+@.+/ and then sending a verification email. If it's important enough to validate, it's important enough to verify, because a legal email address can still belong to someone other than your visitor. Or contain an unintended but fatal typo.

*Edit: removed the dot from the domain part of the regex, because a@to is still a valid email address. So even my super simplified validation rejected valid addresses. Is there any downside at all to just accepting everything that contains an @ with something in front and behind it?

零時差 2024-08-21 06:26:33

关于这个主题的一篇非常好的文章 在阅读 RFC 之前我知道如何验证电子邮件地址

A very good article about this subject I Knew How To Validate An Email Address Until I Read The RFC

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文