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;
}

留言

這個網誌中的熱門文章

7-ELEVEN 電子發票明細查詢方式

Java 使用 regular expression 正則表達,過濾特殊字元,反斜線處理重點

Ubuntu GUI中,無法啟動WIFI連結解決方法