Email and phone Number Validation in android

For Email Address Validation

private boolean isValidMail(String email) {

    String EMAIL_STRING = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    return Pattern.compile(EMAIL_STRING).matcher(email).matches();

}

OR

private boolean isValidMail(String email) {
   return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

For Mobile Validation

For Valid Mobile You need to consider 7 digit to 13 digit because some country have 7 digit mobile number. If your main target is your own country then you can match with the length. Assuming India has 10 digit mobile number. Also we can not check like mobile number must starts with 9 or 8 or anything.

For mobile number I used this two Function:

private boolean isValidMobile(String phone) {
    if(!Pattern.matches("[a-zA-Z]+", phone)) {
        return phone.length() > 6 && phone.length() <= 13;
    }
    return false;
}

OR

private boolean isValidMobile(String phone) {
    return android.util.Patterns.PHONE.matcher(phone).matches();    
}

Leave a Comment