我试图做一个非常简单的电子邮件验证。但问题是,当我尝试类似[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/。这是一个非常好的图书馆。 –