这是有效的电子邮件地址吗?
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据此 MSDN 页面,扩展 ASCII 字符目前无效,但有一个提议的规范可以改变这一点。
http://msdn.microsoft .com/en-us/library/system.net.mail.mailaddress(VS.90).aspx
重要部分在这里:
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:
在本 RFC 中,
ASCII
表示US-ASCII
,即不允许使用值大于 127 的字符。作为证明,以下是 RFC 5321 中的一些引用:这些引号非常清楚地表明值大于 127 的字符被视为
非 ASCII
。由于这些字符在 MAIL TO 或 RCPT 命令中被明确禁止,因此不可能将它们用于电子邮件地址。因此,
"Francoise Lefevre"@example.com
是一个完全有效的地址(根据 RFC),而"Françoise Lefèvre"@example.com
则不是。In this RFC,
ASCII
meansUS-ASCII
, i.e., no characters with a value greater than 127 are allowed. As a proof, here are some quotes from RFC 5321: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.从技术上讲是可以的,但请继续阅读:
...
Technically yes, but read on:
...
HTML5 规范有一个 对有效电子邮件地址问题的有趣看法:
当然,这样做的好处是,您可以查看开源浏览器的 用于验证它的源代码(查找
IsValidEmailAddress
函数)。当然是用C语言写的,但是翻译成JS并不难。The HTML5 spec has an interesting take on the issue of valid email addresses:
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.