【发布时间】:2012-01-05 18:48:57
【问题描述】:
可能重复:
Verify email in Java
我正在尝试做一个非常简单的电子邮件验证。但问题是当我尝试类似tom@@@@gmail.com 时,它返回true。
代码如下:
public static boolean validateEmail(String email){
boolean isValidEmail = false;
// Input the string for validation
// String email = "xyz@.com";
// Set the email pattern string
Pattern p = Pattern.compile(".+@.+\\.[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;
}
请帮忙。提前致谢。
【问题讨论】:
标签: java regex email-validation