How can I validate an email on android [duplicated]

1

I have this code to validate emails:

public static boolean validarEmail(String email) {
    Pattern pattern = Patterns.EMAIL_ADDRESS;
    return pattern.matcher(email).matches();
}

But is there another more complete and comprehensive way to validate them?

    
asked by Alberto Mier 24.11.2017 в 07:54
source

3 answers

0

^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$

I found this expression here which also validates emails of the following type: [email protected]

The example of the email has been contributed by: Mariano

    
answered by 24.11.2017 / 14:07
source
1

You can use this regex in principle and then expand it as it suits you:

    Pattern email_rj = Pattern.compile("^[a-z0-9]+([.][a-z0-9]+)*[@]+[a-z0-9]+[.]+[a-z]{3,4}$");
    boolean valid_em = email_rj.matcher("[email protected]").matches();

Allows you to see exactly what you are validating.

Where are the ñ and other special characters?

Addresses that contain non-ASCII characters are included in a rule called IDN : when you enter one, to be compatible with the current DNS system should be converted to Punycode and then be searched in its ASCII version.

For example if you enter eñe.com , in reality the request sent is xn--ee-zja.com

Something to keep in mind is that, in principle, this is not a fully implemented standard.

Even, each valid email provider how you want the addresses that your users can create.

For example Google only allows characters from the a-z, 0-9 and points.

The safest way to validate an email is sent an email to it with a verification link, in order not to fall into contradictions.

If you only want to warn the user that you entered the wrong email, you can enter the special characters in the regex, assuming now that your provider allows this.

    
answered by 26.11.2017 в 09:20
1

The same Android SDK gives you several patterns of regular expressions, existing from API 8.

To check the email if it is valid use android.util.Patterns.EMAIL_ADDRESS

Encompassing a function isValidEmail("email")

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

Your use

if (isValidEmail("[email protected]") // true o false

    
answered by 26.11.2017 в 13:40