Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android check if an email address is valid or not?

Tags:

java

android

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?

like image 863
Mohammad Rababah Avatar asked Mar 12 '14 10:03

Mohammad Rababah


6 Answers

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();
    }
like image 84
Shayan Pourvatan Avatar answered Oct 29 '22 20:10

Shayan Pourvatan


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();
like image 27
CoderDecoder Avatar answered Oct 29 '22 19:10

CoderDecoder


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.

like image 42
Juan Cortés Avatar answered Oct 29 '22 20:10

Juan Cortés


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.

like image 27
Phantômaxx Avatar answered Oct 29 '22 19:10

Phantômaxx


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.

like image 44
Renjith Krishnan Avatar answered Oct 29 '22 20:10

Renjith Krishnan


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.

like image 39
Theolodis Avatar answered Oct 29 '22 20:10

Theolodis