How to detect email address format - Android, Java coding
1. First import java.util.regex.Pattern.
2. Email regex is ^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$
3. Use Pattern.compile( [email regex string] ).
Finally, coding method like below.
public static boolean checkEmail( final String mail )
{
if( mail != null )
{
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile( EMAIL_PATTERN );
return pattern.matcher( mail ).matches();
}
return false;
}
留言
張貼留言