"Françoise Lefèvre"@example.com
我正在阅读RFC 5321以试图真正理解什么是有效的电子邮件地址——我可能让这比它需要的要困难得多——但这一直困扰着我。
i.e., within a quoted string, any ASCII graphic or space is permitted without blackslash-quoting except double-quote and the backslash itself.
这是否意味着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)——所有这些我都可以轻松地没有。