How can we perform Email Validation
on edittext
in android
? I have gone through google & SO but I didn't find out a simple way to validate it.

- 3,936
- 2
- 23
- 36

- 11,802
- 17
- 73
- 121
-
23android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches(); – Pratik Butani Dec 16 '13 at 10:40
9 Answers
Java:
public static boolean isValidEmail(CharSequence target) {
return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches());
}
Kotlin:
fun CharSequence?.isValidEmail() = !isNullOrEmpty() && Patterns.EMAIL_ADDRESS.matcher(this).matches()
Edit: It will work On Android 2.2+ onwards !!
Edit: Added missing ;

- 3,595
- 4
- 36
- 48

- 8,137
- 4
- 15
- 15
-
71+1 for using built-in function. I would also replace `target == null`with `TextUtils.isEmpty(target)`. – rciovati Apr 13 '13 at 09:02
-
this functionality doesn's validate if i use swipe option while typing any alternative please? – Anitha Mar 10 '15 at 06:07
-
24one line solution `return !TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches();` – Madan Sapkota Jun 20 '15 at 05:44
-
-
You shouldl also add the annotation: `@Contract("null -> false")` add add the import: `import org.jetbrains.annotations.Contract;` – Ali Bdeir Aug 30 '16 at 10:57
-
3This built-in pattern is unfortunately incomplete. For example, "a@a." would pass. Check this question for a "good enough for most" and an RFC822 compliant answer. – MPelletier Jun 18 '17 at 20:50
-
5or a single liner `boolean result = !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();` :) – Faisal Naseer Oct 30 '17 at 10:56
-
5
-
-
-
This doesn't work properly anymore. A valid email should be mason@日本.com but and this pattern matcher will return false. – JoKr Nov 15 '19 at 14:27
-
According to RFC5321 section 4.5.3.1.1, the maximum total length of a user name or other local-part is 64 octets. But `Patterns.EMAIL_ADDRESS` allows input of local part of maximum 256 characters. Any reason of this? (There is a private fields `EMAIL_ADDRESS_LOCAL_PART` which follows the 64 octets limit.) – BakaWaii Dec 01 '20 at 06:15
-
1I would use `isBlank` not `isEmpty` for you cannot have empty space in emails. – Martin Marconcini Mar 23 '21 at 12:27
-
Wouldn't this one alone `Patterns.EMAIL_ADDRESS.matcher(target).matches()` be enough? – MK. May 25 '23 at 02:06
To perform Email Validation we have many ways,but simple & easiest way are two methods.
1- Using EditText(....).addTextChangedListener
which keeps triggering on every input in an EditText box
i.e email_id is invalid or valid
/**
* Email Validation ex:- tech@end.com
*/
final EditText emailValidate = (EditText)findViewById(R.id.textMessage);
final TextView textView = (TextView)findViewById(R.id.text);
String email = emailValidate.getText().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
}
});
2- Simplest method using if-else
condition. Take the EditText box string using getText() and compare with pattern provided for email. If pattern doesn't match or macthes, onClick of button toast a message. It ll not trigger on every input of an character in EditText box . simple example shown below.
final EditText emailValidate = (EditText)findViewById(R.id.textMessage);
final TextView textView = (TextView)findViewById(R.id.text);
String email = emailValidate.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
// onClick of button perform this simplest code.
if (email.matches(emailPattern))
{
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();
}

- 11,802
- 17
- 73
- 121
I did this way:
Add this method to check whether email address is valid or not:
private boolean isValidEmailId(String email){
return Pattern.compile("^(([\\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})$").matcher(email).matches();
}
Now check with String of EditText:
if(isValidEmailId(edtEmailId.getText().toString().trim())){
Toast.makeText(getApplicationContext(), "Valid Email Address.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "InValid Email Address.", Toast.LENGTH_SHORT).show();
}
Done

- 6,627
- 2
- 58
- 83

- 52,124
- 21
- 173
- 151
Use this method for validating your email format. Pass email as string , it returns true if format is correct otherwise false.
/**
* validate your email address format. Ex-akhi@mani.com
*/
public boolean emailValidator(String email)
{
Pattern pattern;
Matcher matcher;
final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}

- 3,502
- 5
- 28
- 59
-
1
-
1marks these as invalid "plainaddress", "@domain.com", "あいうえお@domain.com", "email@domain", "email@111.222.333.44444", "email@domain..com" – martinseal1987 May 10 '20 at 07:16
Try this:
if (!emailRegistration.matches("[a-zA-Z0-9._-]+@[a-z]+\.[a-z]+")) {
editTextEmail.setError("Invalid Email Address");
}

- 1,009
- 1
- 7
- 22

- 743
- 1
- 5
- 24
-
2This won't work for emails with multiple dots in domain like a@woof.co.za . Also it matches things with no dots like name@gmailcom – Matiaan Apr 19 '15 at 09:51
-
There you go, fixed your regex to include the literal dot instead of "accept any character here" dot. – zombiesauce Jul 25 '20 at 16:52
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;
}

- 8,621
- 10
- 63
- 98
-
this worked perfectly for me,may be the expression makes the difference.. – Sagar G. Jun 20 '13 at 10:22
Use this method to validate the EMAIL :-
public static boolean isEditTextContainEmail(EditText argEditText) {
try {
Pattern pattern = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
Matcher matcher = pattern.matcher(argEditText.getText());
return matcher.matches();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
Let me know if you have any queries ?

- 8,282
- 21
- 88
- 143
try this
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}" +
")+"
);
and in tne edit text
final String emailText = email.getText().toString();
EMAIL_ADDRESS_PATTERN.matcher(emailText).matches()

- 3,738
- 4
- 29
- 50
-
This is android pattern and accepting 5125945@12.2121. Do you have any solution regarding this scenario – Amarjit Nov 01 '16 at 12:17
This is a sample method i created to validate email addresses, if the string parameter passed is a valid email address , it returns true, else false is returned.
private boolean validateEmailAddress(String emailAddress){
String expression="^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = emailAddress;
Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
return matcher.matches();
}

- 719
- 3
- 6