I need to validate an email pattern which starts with an alphabat
and ends with @gmail.com
. Following is my code.
public static boolean isValidEmail(String email)
{
String endPattern = "@gmail.com";
if(null == email){
return false;
}
if(email.length()<10){
return false;
}
if(!email.endsWith(endPattern)){
return false;
}
String[] strArr = email.split(endPattern);
String mailId = strArr[0];
if(!Character.isLetter((mailId.charAt(0)))){
return false;
}
return true;
}
Is there a better way to acheive this? A regex or a better code?
Use a regex
public static boolean isValidEmail(String email)
{
if (email != null)
{
Pattern p = Pattern.compile("^[A-Za-z].*?@gmail\\.com$");
Matcher m = p.matcher(email);
return m.find();
}
return false;
}