What's a good technique for validating an e-mail address (e.g. from a user input field) in Android? org.apache.commons.validator.routines.EmailValidator doesn't seem to be available. Are there any other libraries doing this which are included in Android already or would I have to use RegExp?
Another option is the built in Patterns starting with API Level 8:
public final static boolean isValidEmail(CharSequence target) { if (TextUtils.isEmpty(target)) { return false; } else { return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); } }
Patterns viewable source
OR
One line solution from @AdamvandenHoven:
public final static boolean isValidEmail(CharSequence target) { return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); }
Next pattern is used in K-9 mail:
public static final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile( "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\." + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+" );
You can use function
private boolean checkEmail(String email) { return EMAIL_ADDRESS_PATTERN.matcher(email).matches(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With