"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 extended character sets是引号内有效?或者这仅暗示standard ASCII table?
编辑 - 考虑到这些问题的答案,下面是一个简单的jQuery validator,它可以用来补充插件的内置电子邮件验证以检查字符。
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");
该插件的内置验证似乎很不错,除了捕获无效字符。在here列出的测试用例中,它仅禁止评论,折叠空白和缺少TDL的地址(例如:@localhost,@ 255.255.255.255) - 我可以轻松地在这些地方生活。
一般来说,这类问题的最佳答案是地址是有效的,如果你可以让两个不同的MTA接受它。 IETF标准并不总是按照您的意愿明确地指定事物。 – msw 2010-08-12 12:57:14
不要验证单个字符。 [确定语法](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses/1931322#1931322)。 – BalusC 2010-08-12 13:59:35
@BafusC我* *验证语法。我也想阻止人们将梵文填入只有ASCII的字段中。这两者不是相互排斥的。不过,我确实认识到,使用RegEx进行真正的电子邮件验证就像一个redditer所说的那样,“就像建造一栋仅使用电钻的房屋一样。”客户端验证只是为了告诉某人“嘿,这不属于” - 我相信这是一个很好的,简单的方法。 – Greg 2010-08-12 14:03:37