这是有效的电子邮件地址吗?

发布于 2024-09-13 16:53:22 字数 1742 浏览 8 评论 0原文

"Françoise Lefèvre"@example.com

我正在阅读 RFC 5321 尝试真正理解有效电子邮件地址的构成 - - 我可能会让这变得比需要的更加困难 - 但这一直困扰着我。

 即,在带引号的字符串中,任何
               允许使用 ASCII 图形或空格
               没有黑斜杠引用,除了
               双引号和反斜杠本身。

这是否意味着 ASCII 扩展字符集 在引号内有效?或者这是否仅意味着标准 ASCII 表

编辑 - 考虑到答案,这是一个简单的 jQuery 验证器这可以作为插件内置电子邮件验证的补充来检查字符。

jQuery.validator.addMethod("ascii_email", function( value, element ) { 
    // In compliance with RFC 5321, this allows all standard printing ASCII characters in quoted text.
    // Unquoted text must be ASCII-US alphanumeric or one of the following: ! # $ % & ' * + - / = ? ^ _ ` { | } ~   
    // @ and . get a free pass, as this is meant to be used together with the email validator

    var result = this.optional(element) || 
        (
            /^[\u002a\u002b\u003d\u003f\u0040\u0020-\u0027\u002d-u002f\u0030-\u0039\u0041-\u005a\u005e-\u007e]+$/.test(value.replace(/(["])(?:\\\1|.)*?\1/, "")) &&     
            /^[\u0020-\u007e]+$/.test(value.match(/(["])(?:\\\1|.)*?\1/, ""))   
        );
    return result;
}, "Invalid characters");

除了捕获无效字符之外,该插件的内置验证似乎相当不错。在此处列出的测试用例中,它仅不允许注释、折叠空格以及缺少 TDL 的地址(即:@localhost、@255.255.255.255)——所有这些我都可以轻松生活。

"Françoise Lefèvre"@example.com

I'm reading RFC 5321 to try to actually understand what constitutes a valid email address -- and I'm probably making this a lot more difficult than it needs to be -- but this has been bugging me.

               i.e., within a quoted string, any
               ASCII graphic or space is permitted
               without blackslash-quoting except
               double-quote and the backslash itself.

Does this mean that ASCII extended character sets are valid within quotes? Or does that imply standard ASCII table only?

EDIT - With the answers in mind, here's a simple jQuery validator that could work in supplement to the the plugin's built-in email validation to check the characters.

jQuery.validator.addMethod("ascii_email", function( value, element ) { 
    // In compliance with RFC 5321, this allows all standard printing ASCII characters in quoted text.
    // Unquoted text must be ASCII-US alphanumeric or one of the following: ! # $ % & ' * + - / = ? ^ _ ` { | } ~   
    // @ and . get a free pass, as this is meant to be used together with the email validator

    var result = this.optional(element) || 
        (
            /^[\u002a\u002b\u003d\u003f\u0040\u0020-\u0027\u002d-u002f\u0030-\u0039\u0041-\u005a\u005e-\u007e]+$/.test(value.replace(/(["])(?:\\\1|.)*?\1/, "")) &&     
            /^[\u0020-\u007e]+$/.test(value.match(/(["])(?:\\\1|.)*?\1/, ""))   
        );
    return result;
}, "Invalid characters");

The plugin's built-in validation appears to be pretty good, except for catching invalid characters. Out of the test cases listed here it only disallows comments, folding whitespace and addresses lacking a TDL (ie: @localhost, @255.255.255.255) -- all of which I can easily live without.

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

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

发布评论

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

评论(4

删除→记忆 2024-09-20 16:53:22

根据此 MSDN 页面,扩展 ASCII 字符目前无效,但有一个提议的规范可以改变这一点。

http://msdn.microsoft .com/en-us/library/system.net.mail.mailaddress(VS.90).aspx

重要部分在这里:

托马斯·李 (Thomas Lee) 所引用的内容是正确的
本地部分在电子邮件中有效
地址和某些邮件地址可能
如果不在带引号的字符串中则无效。
然而,其他人认为的角色
你提到过诸如元音变音
龙舌兰不在 ASCII 中
字符集,它们被扩展
ASCII。在 RFC 2822(及后续
RFC 的 5322 和 3696) dtext
规范(允许在引用的本地
部分)只允许大多数 ASCII 值
(RFC 2822,第 3.4.1 节)
包括 33-90 范围内的值
和94-126。 RFC 5335 已被提议
这将允许非 ASCII 字符
在地址规范中,但它仍然是
标记为实验性的,因此是
MailAddress 不支持。

According to this MSDN page the extended ASCII characters aren't valid, currently, but there is a proposed specification that would change this.

http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress(VS.90).aspx

The important part is here:

Thomas Lee is correct in that a quoted
local part is valid in an email
address and certain mail addresses may
be invalid if not in a quoted string.
However, the characters that others of
you have mentioned such as the umlaut
and the agave are not in the ASCII
character set, they are extended
ASCII. In RFC 2822 (and subsequent
RFC's 5322 and 3696) the dtext
specification (allowed in quoted local
parts) only allows most ASCII values
(RFC 2822, section 3.4.1) which
includes values in ranges from 33-90
and 94-126. RFC 5335 has been proposed
that would allow non-ascii characters
in the addr-spec, however it is still
labeled as experimental and as such is
not supported in MailAddress.

沩ん囻菔务 2024-09-20 16:53:22

在本 RFC 中, ASCII 表示 US-ASCII ,即不允许使用值大于 127 的字符。作为证明,以下是 RFC 5321 中的一些引用:

邮件数据可能包含 128 个 ASCII 字符代码中的任何一个,[...]

[...]

系统定义邮箱时不得要求在 SMTP 中使用非 ASCII 字符(高位设置为 1 的八位字节)或 ASCII“控制字符”(十进制值 0-31 和 127) 。这些字符不得在 MAIL 或 RCPT 命令或其他需要邮箱名称的命令中使用。

这些引号非常清楚地表明值大于 127 的字符被视为非 ASCII。由于这些字符在 MAIL TO 或 RCPT 命令中被明确禁止,因此不可能将它们用于电子邮件地址。

因此,"Francoise Lefevre"@example.com 是一个完全有效的地址(根据 RFC),而 "Françoise Lefèvre"@example.com 则不是。

In this RFC, ASCII means US-ASCII , i.e., no characters with a value greater than 127 are allowed. As a proof, here are some quotes from RFC 5321:

The mail data may contain any of the 128 ASCII character codes, [...]

[...]

Systems MUST NOT define mailboxes in such a way as to require the use in SMTP of non-ASCII characters (octets with the high order bit set to one) or ASCII "control characters" (decimal value 0-31 and 127). These characters MUST NOT be used in MAIL or RCPT commands or other commands that require mailbox names.

These quotes quite clearly imply that characters with a value greater than 127 are considered non-ASCII. Since such characters are explicitly forbidden in MAIL TO or RCPT commands, it is impossible to use them for e-mail addresses.

Thus, "Francoise Lefevre"@example.com is a perfectly valid address (according to the RFC), whereas "Françoise Lefèvre"@example.com is not.

九公里浅绿 2024-09-20 16:53:22

从技术上讲是可以的,但请继续阅读:

虽然上面的定义
本地部分相对宽松,
为了最大程度地实现互操作性,主机
期望收到邮件的应该
避免定义邮箱
本地部分需要(或使用)
带引号的字符串形式或其中
本地部分区分大小写。

...

系统不得在以下位置定义邮箱
这样的方式要求使用
非 ASCII 字符的 SMTP。

Technically yes, but read on:

While the above definition for
Local-part is relatively permissive,
for maximum interoperability, a host
that expects to receive mail SHOULD
avoid defining mailboxes where the
Local-part requires (or uses) the
Quoted-string form or where the
Local-part is case- sensitive.

...

Systems MUST NOT define mailboxes in
such a way as to require the use in
SMTP of non-ASCII characters.

送君千里 2024-09-20 16:53:22

HTML5 规范有一个 对有效电子邮件地址问题的有趣看法

有效的电子邮件地址是与 ABNF 产生式 1*( atext / "." ) "@" ldh-str 1*( "." ldh-str ) 匹配的字符串,其中 atext 在 RFC 5322 部分中定义3.2.3,ldh-str 在 RFC 1034 第 3.5 节中定义。

当然,这样做的好处是,您可以查看开源浏览器的 用于验证它的源代码(查找IsValidEmailAddress函数)。当然是用C语言写的,但是翻译成JS并不难。

The HTML5 spec has an interesting take on the issue of valid email addresses:

A valid e-mail address is a string that matches the ABNF production 1*( atext / "." ) "@" ldh-str 1*( "." ldh-str ) where atext is defined in RFC 5322 section 3.2.3, and ldh-str is defined in RFC 1034 section 3.5.

The nice thing about this, of course, is that you can then take a look at the open source browser's source code for validating it (look for the IsValidEmailAddress function). Of course it's in C, but not too hard to translate to JS.

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