How should I validate an e-mail address?

znq picture znq · Nov 30, 2009 · Viewed 234.2k times · Source

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?

Answer

mindriot picture mindriot · Oct 25, 2011

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