0

I had created an login register form in which i want to edit text to insert email address i had used input type text email address put it does not check weather its an valid email format or not can any tell how to check the email format in android thanks in advance

enter code here<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView2"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/editText1"
    android:ems="10"
    android:inputType="textEmailAddress" />
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1922355
  • 39
  • 2
  • 4
  • 12

9 Answers9

13

refer to this how to check edittext's text is email address or not?

I quote the most ticked answer, which i believe it is the most elegant.

On Android 2.2+ use this:

boolean isEmailValid(CharSequence email) {
   return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
Community
  • 1
  • 1
iversoncru
  • 579
  • 8
  • 22
3

You can use regular expression (Regex) to check the email pattern.

Pattern pattern1 = Pattern.compile( "^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\\.([a-zA-Z])+([a-zA-Z])+");

Matcher matcher1 = pattern1.matcher(Email);

if (!matcher1.matches()) {
    //show your message if not matches with email pattern
}
Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44
anddevmanu
  • 1,459
  • 14
  • 19
1
**Please follow the following Steps**

    Seet - 1

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_below="@+id/textView_email"
        android:layout_marginTop="40dp"
        android:hint="Email Adderess"
        android:inputType="textEmailAddress" />

    <TextView
        android:id="@+id/textView_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Email Validation Example" />

</RelativeLayout>
  Seet - 2

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
 Seet - 3

public class MainActivity extends Activity {

private EditText email;

private String valid_email;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initilizeUI();
}

/**
 * This method is used to initialize UI Components
 */
private void initilizeUI() {
    // TODO Auto-generated method stub

    email = (EditText) findViewById(R.id.editText_email);

    email.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

            // TODO Auto-generated method stub
            Is_Valid_Email(email); // pass your EditText Obj here.
        }

        public void Is_Valid_Email(EditText edt) {
            if (edt.getText().toString() == null) {
                edt.setError("Invalid Email Address");
                valid_email = null;
            } else if (isEmailValid(edt.getText().toString()) == false) {
                edt.setError("Invalid Email Address");
                valid_email = null;
            } else {
                valid_email = edt.getText().toString();
            }
        }

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

}

}

Rashid Ali
  • 561
  • 6
  • 14
1

Following this article

Method-1) Following works for android 2.2 onwards

    public final static boolean isValidEmail(CharSequence target) {
    if (target == null) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}

Method-2) Using Regular Expressions and adding the validation to textChangeListener of EditText:

 EdiText emailValidate;
String email = emailValidate.getEditableText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";

emailValidate .addTextChangedListener(new TextWatcher() { 
    public void afterTextChanged(Editable s) { 

    if (email.matches(emailPattern) && s.length() > 0)
        { 
            Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
            // or
            textView.setText("valid email");
        }
        else
        {
             Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
            //or
            textView.setText("invalid email");
        }
    } 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // other stuffs 
    } 
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    // other stuffs 
    } 
}); 

Method-3

    public static boolean isEmailValid(String email) {
    boolean isValid = false;

    String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    CharSequence inputStr = email;

    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}

Method-4

if (!emailRegistration.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+")) {

                       edttextEmail.setError("Invalid Email Address");

                   }
Satheesh
  • 1,722
  • 25
  • 35
0

Pass Email id in it:-

      public static boolean emailAddressValidator(String emailId) {
    Pattern pattern = Pattern.compile("\\w+([-+.]\\w+)*" + "\\@"
            + "\\w+([-.]\\w+)*" + "\\." + "\\w+([-.]\\w+)*");

    Matcher matcher = pattern.matcher(emailId);
    if (matcher.matches())
        return true;
    else
        return false;
}
Deepanker Chaudhary
  • 1,694
  • 3
  • 15
  • 35
0

Pass EditText to this method this will return true if E-mail address is valid else it will return false

/**
 * method is used for checking valid email id format.
 * 
 * @param email
 * @return boolean true for valid false for invalid
 */
public static boolean isEmailAddressValid(String email) {
    boolean isEmailValid = false;

    String strExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
    CharSequence inputStr = email;

    Pattern objPattern = Pattern.compile(strExpression , Pattern.CASE_INSENSITIVE);
    Matcher objMatcher = objPattern .matcher(inputStr);
    if (objMatcher .matches()) {
        isEmailValid = true;
    }
    return isEmailValid ;
}
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
0

Here is very good link for Android form edit text is an extension of EditText that brings data validation facilities to the edittext.

It provides the custom validation for edit-text values (Example- Email,number,phone,credit card etc.) May this is useful to you..

Community
  • 1
  • 1
Swapnil Sonar
  • 2,232
  • 2
  • 29
  • 42
0

You can use blow expression also:

^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,3}$
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
0
private TextInputLayout textInputPassword;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textInputEmail = findViewById(R.id.text_input_email);
   
   
}
private boolean validateEmail() {
    String emailInput = textInputEmail.getEditText().getText().toString().trim();
    if (emailInput.isEmpty()) {
        textInputEmail.setError("Field can't be empty");
        return false;
    } else if (!Patterns.EMAIL_ADDRESS.matcher(emailInput).matches()) {
        textInputEmail.setError("Please enter a valid email address");
        return false;
    } else {
        textInputEmail.setError(null);
        return true;
    }
}

u can check here for detailed solution: https://codinginflow.com/tutorials/android/validate-email-password-regular-expressions

  • 1
    Thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. – Jeremy Caney Feb 02 '21 at 19:16