How should I validate an e-mail address?

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?

6,525 17 17 gold badges 70 70 silver badges 123 123 bronze badges asked Nov 30, 2009 at 11:01 44.9k 41 41 gold badges 118 118 silver badges 145 145 bronze badges please refers this one, may it will help you: stackoverflow.com/questions/12947620/… Commented Apr 15, 2013 at 4:48

@user2757064 well this question should help the other question you linked. That question was asked 3 years after this. :)

Commented Aug 15, 2014 at 7:44 for simple check: if(!Patterns.EMAIL_ADDRESS.matcher(editTextEmail.getText().toString()).matches()) < //show toast:enter valid email > Commented Dec 2, 2022 at 19:08

35 Answers 35

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

OR

One line solution from @AdamvandenHoven:

public final static boolean isValidEmail(CharSequence target)
answered Oct 24, 2011 at 22:59 14.3k 4 4 gold badges 30 30 silver badges 40 40 bronze badges +1 , but i prefer replace the (target == null) with TextUtils.isEmpty(target) :) Commented Dec 16, 2013 at 17:40

Answers like these are why I dislike accepted answers being shown on top, instead of answers with most votes.

Commented Jan 12, 2014 at 19:58

Would it also not make more sense (readability wise) to simply combine this into one line: return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();

Commented Apr 17, 2014 at 16:35

@Houcine, true but the method above returns boolean so we don't have that information when it is false. Anyway, one can toast or update ui from inside the function(must be run in UI thread). For those like me, who are validating an email obtained programmatically and with no user interaction whatsoever, we can just stick with '== null' and maybe save a couple of clock cycles in most cases.

Commented Dec 28, 2014 at 11:52 Be careful. This matcher accepts [email protected] as a valid email Commented Feb 9, 2017 at 17:18

Next pattern is used in K-9 mail:

public static final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile( "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]" + "\\@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]" + "(" + "\\." + "[a-zA-Z0-9][a-zA-Z0-9\\-]" + ")+" ); 

You can use function

private boolean checkEmail(String email)
1,541 3 3 gold badges 24 24 silver badges 37 37 bronze badges answered Mar 14, 2011 at 8:19 Andrei Buneyeu Andrei Buneyeu 6,670 6 6 gold badges 36 36 silver badges 38 38 bronze badges Why not use android.util.Patterns.EMAIL_ADDRESS ? Commented Mar 18, 2012 at 13:56 cause it exists since API Level 8 only Commented Mar 20, 2012 at 7:59 this helped me a lot thanks and +1 for the simple answer.. :) Commented Feb 11, 2013 at 10:04

FYI: inside character classes the metacharacters much fewer and the hyphen is automatically escaped when put at a border, you can simplify the first class to [a-zA-Z0-9+._%-] and the others to [a-zA-Z0-9-]

Commented Sep 3, 2014 at 10:57

This only return true if the email syntax is the same as email does, I tried to remove i from @gmail.com it will return true. Same in `@yaho.com .

Commented Sep 25, 2016 at 8:14

Since API 8 (android 2.2) there is a pattern: android.util.Patterns.EMAIL_ADDRESS http://developer.android.com/reference/android/util/Patterns.html

So you can use it to validate yourEmailString:

private boolean isValidEmail(String email)

returns true if the email is valid

UPD: This pattern source code is:

public static final Pattern EMAIL_ADDRESS = Pattern.compile( "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]" + "\\@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]" + "(" + "\\." + "[a-zA-Z0-9][a-zA-Z0-9\\-]" + ")+" ); 

So you can build it yourself for compatibility with API < 8.

answered Apr 9, 2012 at 7:56 5,630 5 5 gold badges 27 27 silver badges 24 24 bronze badges "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]" Why did they repeat '+' twice? Commented May 19, 2022 at 3:34

We have a simple Email pattern matcher now.

Java:

 private static boolean isValidEmail(String email)

Kotlin Function:

 private fun isValidEmail(email: String): Boolean

Kotlin Extension:

fun String.isValidEmail() = !TextUtils.isEmpty(this) && Patterns.EMAIL_ADDRESS.matcher(this).matches() 
answered May 10, 2016 at 15:29 Salman Nazir Salman Nazir 2,817 2 2 gold badges 29 29 silver badges 42 42 bronze badges

Don't use a reg-ex.

Apparently the following is a reg-ex that correctly validates most e-mails addresses that conform to RFC 2822, (and will still fail on things like "[email protected]", as will org.apache.commons.validator.routines.EmailValidator)

(?:[a-z0-9!#$%&'*+/=?^_`<|>~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`<|>~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.)(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]) 

Possibly the easiest way to validate an e-mail to just send a confirmation e-mail to the address provided and it it bounces then it's not valid.

If you want to perform some basic checks you could just check that it's in the form *@*

If you have some business logic specific validation then you could perform that using a regex, e.g. must be a gmail.com account or something.