"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