在我看來,你想驗證現在使用的電子郵件ID,而不是真正符合任何RFC。 對於我們的項目,我們製作了我們自己的非常簡單的電子郵件驗證器。爲什麼? apache和java郵件使用正則表達式,並且有一些情況(我不知道哪些是因爲我們不打印電子郵件的日誌),這使得正則表達式進入永久循環。這意味着客戶端處理程序線程進入循環並且用戶看到一個空白屏幕!
因此,我們所做的基本上是允許新的電子郵件ID,就像他們在像谷歌/雅虎這樣的網站看起來像。
含義[email protected]
我們檢查 最多1 @ 檢查1個或更多字符@前 後@什麼有一些字符+一個點ATLEAST點後+ CHRS
過去兩年沒有收到任何投訴。此外,大多數情況下,您需要向該人發送電子郵件以確保域名存在等,並使用唯一標記鏈接(註冊確認)以確保該人擁有該電子郵件標識(向真實所有者發送消息點擊,如果他們來到您的網站)
/**
* minimum email [email protected]
* */
public static boolean checkEmail(final String emlId, int dbgPrint) {
// ex:[email protected]
if (emlId == null){
return false;
}
final int lngth = emlId.length();
if (lngth < 6) {
if (dbgPrint > 1) {
System.out.println(" lngth < 6");
}
return false;
}
final int locationAt = emlId.indexOf('@');
if (locationAt < 1) {
if (dbgPrint > 1) {
System.out.println("locationAt < 1 : " + locationAt);
}
return false;//
}
final int postLastPeriod = emlId.lastIndexOf('.');
if (postLastPeriod < 0) {
if (dbgPrint > 1) {
System.out.println("postLastPeriod < 0, locationAt " + locationAt);
}
return false;
}
if (dbgPrint > 1) {
System.out.println(" locationAt " + locationAt + ", postLastPeriod :" + postLastPeriod + " lngth " + lngth);
}
if (lngth - postLastPeriod < 3) {
if (dbgPrint > 1) {
System.out.println(" lngth - postLastPeriod < 2");
}
return false;
}
if (postLastPeriod - locationAt < 1) {
if (dbgPrint > 1) {
System.out.println(" postLastPeriod - locationAt < 1");
}
return false;
}
return true;
}
你確定'ar @ yahoo'應該是無效的嗎?我會檢查一個通用的頂級域名是否可以用作主機名稱。 – Matteo 2013-05-09 07:41:27
並且文檔中說:「驗證地址部分」,所以我猜'@'後面的所有內容都會被忽略。 – Matteo 2013-05-09 07:42:53
在前面的問題中,Apache Commons被認爲是最好的工具(http://stackoverflow.com/questions/624581/what-is-the-best-java-email-address-validation-method),但我只是試了一下而已也未能接受評論。 – Matteo 2013-05-09 07:56:09