为什么 jQuery 验证电子邮件正则表达式允许以“.”结尾的电子邮件

发布于 2024-12-05 11:19:44 字数 1433 浏览 1 评论 0原文

我们喜欢 jQuery validate,并且真的想继续使用它。

然而,它最有用的功能之一是能够在客户端验证电子邮件地址...但是它使用的正则表达式接受以“.”结尾的电子邮件,我认为这不是有效的,当然也不是有效的受到我们后端系统的喜爱。

我想知道是否有人知道为什么选择这个特定的正则表达式或者它是否可能是一个错误?

有问题的 jquery.validate-1.8.1 代码:

        // http://docs.jquery.com/Plugins/Validation/Methods/email
    email: function(value, element) {
        // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
        return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
    },

We love jQuery validate, and really want to keep using it.

However, one of it's most useful features is to be able to validate email addresses client-side... but the regular expression that it uses accepts emails ending in ".", which I didn't believe was valid and certainly isn't liked by our back-end systems.

I wonder if anyone knows why this particular regex was chosen or whether it is perhaps a bug?

The jquery.validate-1.8.1 code in quesion:

        // http://docs.jquery.com/Plugins/Validation/Methods/email
    email: function(value, element) {
        // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
        return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
    },

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

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

发布评论

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

评论(2

灼疼热情 2024-12-12 11:19:45

我认为此页面将解释为什么允许在末尾使用点:

http://en.wikipedia.org/维基/Fully_qualified_domain_name

完全限定域名 (FQDN),有时也称为
绝对域名,[1]是指定其确切的域名
在域名系统 (DNS) 的树形层次结构中的位置。它
指定所有域级别,包括顶级域和
根域。完全合格的域名的特点是
明确;它只能以一种方式解释。

例如,给定一个具有本地主机名 myhost 和父级的设备
域名 example.com,完全限定域名是
myhost.example.com。因此,FQDN 唯一标识设备
—虽然世界上可能有很多名为 myhost 的主机,但也可以
只能是一个 myhost.example.com。 在域名系统中,大多数
值得注意的是,在 DNS 区域文件中,指定了完全限定的域名
带有尾随点。
例如,somehost.example.com。指定一个
以空顶级域标签结尾的绝对域名。

DNS根域未命名,用空标签表示,
生成以点分隔符结尾的域名。

I think this page will explain why the dot is allowed at the end:

http://en.wikipedia.org/wiki/Fully_qualified_domain_name

A fully qualified domain name (FQDN), sometimes also referred as
absolute domain name,[1] is a domain name that specifies its exact
location in the tree hierarchy of the Domain Name System (DNS). It
specifies all domain levels, including the top-level domain and the
root domain. A fully qualified domain name is distinguished by its
unambiguity; it can only be interpreted one way.

For example, given a device with a local hostname myhost and a parent
domain name example.com, the fully qualified domain name is
myhost.example.com. The FQDN therefore uniquely identifies the device
—while there may be many hosts in the world called myhost, there can
only be one myhost.example.com. In the Domain Name System, and most
notably, in DNS zone files, a fully qualified domain name is specified
with a trailing dot.
For example, somehost.example.com. specifies an
absolute domain name that ends with an empty top level domain label.

The DNS root domain is unnamed, which is expressed by an empty label,
resulting in a domain name ending with the dot separator.

再可℃爱ぅ一点好了 2024-12-12 11:19:44

stackoverflow.com. 完全有效 - 实际上这是使域名成为 完全合格的域名。从技术上讲,这是您的后端系统不接受此错误的原因(尽管我确信我自己过去编写过不接受它的验证代码)要么...)

请参阅http://www.dns-sd.org/TrailingDotsInDomainNames.html 进行深入解释。

stackoverflow.com. is perfectly valid - actually it's the only way to make a domain name a Fully Qualified Domain Name. Technically, it's your back-end systems that are at fault for not accepting this (although I'm sure I myself have written validation code in the past that doesn't accept it either...)

See http://www.dns-sd.org/TrailingDotsInDomainNames.html for an in-depth explanation.

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