Possible Duplicate:
Verify email in Java
I'm trying to do a very simple email validation. But the problem is when I try something like tom@@@@gmail.com
it returns true.
Here are the codes:
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;
}
Please help. Thanks in advance.