39

I have this EditText definition:

<EditText 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"
     android:inputType="textEmailAddress" 
     android:id="@+id/EmailText"/>

Notice the EditText has the inputType defined with an email address specification. Does Android have anything built in to validate an email address input type, or does this all have to be done manually? It's allowing me to enter invalid data, so I'm curious as to its purpose.

Thanks.

Patel Pinkal
  • 8,984
  • 4
  • 28
  • 50
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • please refers this one, may it will helpful to you: http://stackoverflow.com/questions/12947620/email-address-validation-in-android-on-edittext – Hiren Patel Apr 15 '13 at 04:50

4 Answers4

165

If you are using API 8 or above, you can use the readily available Patterns class to validate email. Sample code:

public final static boolean isValidEmail(CharSequence target) {
    if (target == null) 
        return false;

    return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}

By chance if you are even supporting API level less than 8, then you can simply copy the Patterns.java file into your project and reference it. You can get the source code for Patterns.java from this link

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
46

Here By giving input type Email you are setting the keyboard of email type means "@" and "." keyword will display on key board.

the better solution is to compare the email by following function

public boolean isEmailValid(String email)
    {
         String regExpn =
             "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
                 +"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                   +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
                   +"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
                   +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                   +"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";

     CharSequence inputStr = email;

     Pattern pattern = Pattern.compile(regExpn,Pattern.CASE_INSENSITIVE);
     Matcher matcher = pattern.matcher(inputStr);

     if(matcher.matches())
        return true;
     else
        return false;
}

if this function returns true then your email address is valid otherwise not

James Tan
  • 1,336
  • 1
  • 14
  • 32
dilipkaklotar
  • 1,469
  • 3
  • 20
  • 28
  • Assumed that this method may be called more than once, it's more efficient to store the pattern in a static field. Also, since your pattern uses [a-zA-Z] the flag `Pattern.CASE_INSENSITIVE` is redundant (or you could just use [a-z] with it): `private static final String EMAIL_PATTERN = Pattern.compile("");` Also, the last if-statement can be reduced to: `return matcher.matches()`. And you missed some type declarations ;) – Michel Jung Oct 28 '14 at 14:54
  • 3
    Not a bad solution, but the one right below this is better. – frankelot Jan 30 '15 at 15:45
  • Not passing a valid 1@ggg.ccc. Best alternative is answer bellow or use this pattern String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" // + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; – fox Sep 19 '15 at 10:28
  • it will be easier to work with if you have butterknife and android Saripaar, code looks cleaner :) – James Tan Dec 17 '15 at 04:46
18

a bit better answers you can find here and here (credits to original authors)

boolean isEmailValid(CharSequence email) {
   return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

or custom validation library https://github.com/vekexasia/android-form-edittext check second link for details and preview (regexp, numeric, alpha, alphaNumeric, email, creditCard, phone, domainName, ipAddress, webUrl)

Cheers

Community
  • 1
  • 1
Ewoks
  • 12,285
  • 8
  • 58
  • 67
6

Editext field will not validate your email only by setting it's input method to email type.

You need to validate it yourself.

Android: are there any good solutions how to validate Editboxes

email validation android

Community
  • 1
  • 1
Lucifer
  • 29,392
  • 25
  • 90
  • 143