"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) - 所有这些我都可以轻松忍受。