Reputation: 1726
I develop an android app that can send emails to users.
But it turned out that some email addresses are not valid.
How check if the email address entered by the user is valid or not?
Upvotes: 8
Views: 19763
Reputation: 468
Here is a Kotlin version using Kotlin Extensions (extending the String object, so you can call stringName.isValidEmail()
:
fun String.isValidEmail(): Boolean {
return android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches()
}
Upvotes: 0
Reputation: 5550
Use :
if (!Patterns.EMAIL_ADDRESS.matcher(your_edit_text.getText().toString()).matches()){
loginEmail.setError("Please enter a Valid E-Mail Address!");
}else {
//email is valid
}
Upvotes: 1
Reputation: 11948
if you want check validate of email you can use:
public static boolean isValidEmail(CharSequence target) {
if (target == null) {
return false;
} else {
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}
but there is no way to find out what email address is real
you can use following method for removing if/else.
public static boolean isValidEmail(CharSequence target) {
return target != null && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
Upvotes: 30
Reputation: 445
This code works totally fine and it gives you a boolean value. So if its true it will give true and false it will give you false
android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
Upvotes: 12
Reputation: 38098
You can use a Regular expression to validate an email address, so:
public boolean isEmailValid(String email)
{
final String EMAIL_PATTERN =
"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
final Pattern pattern = Pattern.compile(EMAIL_PATTERN);
final Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
Here is a link with more RegExes to choose from.
Upvotes: 2
Reputation: 1967
Use below code:
if(validateEmail(mEdtTxtEmail.getText().toString().trim())){
// your code
}
private boolean validateEmail(String data){
Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+");
Matcher emailMatcher = emailPattern.matcher(data);
return emailMatcher.matches();
}
Upvotes: 1
Reputation: 5102
There is no way to know if an email exists or not. Specially if it is on a site like yopmail or similar, which I believe would accept any mail to any account on their domain.
However, you can check:
1. if the address has the correct syntax and is on a registered domain (regex for syntax and a dns check if there is a mailserver for the site behind the @
)
2. send an e-mail and check the response, some providers might send you back an error if the mail is not registered on their site.
Upvotes: 1
Reputation: 21112
Probably the best way to check if the email is real, is to actually send an email to that address with a verification code/link that will activate that user on your site. Using regular expressions will only make sure the email is valid, but not necessarily real.
Upvotes: 2
Reputation: 2616
There is no way of checking email is real or not. But You can check only the validation that is it in correct format or not.
Upvotes: 1