我試圖做一個非常簡單的電子郵件驗證。但問題是,當我嘗試類似[email protected]@@@gmail.com
它返回true。
這裏是代碼:
public static boolean validateEmail(String email){
boolean isValidEmail = false;
// Input the string for validation
// String email = "[email protected]";
// Set the email pattern string
Pattern p = Pattern.compile("[email protected]+\\.[a-z]+");
// Match the given string with the pattern
Matcher m = p.matcher(email);
// check whether match is found
boolean matchFound = m.matches();
StringTokenizer st = new StringTokenizer(email, ".");
String lastToken = null;
while (st.hasMoreTokens()) {
lastToken = st.nextToken();
}
if (matchFound && lastToken.length() >= 2
&& email.length() - 1 != lastToken.length()) {
// validate the country code
isValidEmail = true;
}
else isValidEmail = false;
return isValidEmail;
}
請幫助。提前致謝。
我該如何使用Apache Commons?這是我第一次聽說它。 – NinjaBoy
@ChickenBoy:http://commons.apache.org/。這是一個非常好的圖書館。 –