Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.util.Patterns.EMAIL_ADDRESS is validating invalid emails

These are few emails which are not valid.

[email protected]
[email protected]

I have checked above emails in following websites, All of those returns invalid. http://isemail.info/about http://sqa.fyicenter.com/Online_Test_Tools/Email_Address_Format_Validator.php.

Yet, android.util.Patterns.EMAIL_ADDRESS pattern validates both.

Is there a bug or am I missing something?

like image 895
Nidhi Avatar asked Apr 07 '15 12:04

Nidhi


3 Answers

Please check you are using right condition means you use same if condition. because some times we use vice-versa. I know you are using the same thing but please check this method. I am sure it will be helpful for you.

/* returns true if the email is valid otherwise return false */

@SuppressLint("DefaultLocale")
public boolean checkForEmail(EditText edit, String editName) {
    String str = edit.getText().toString();
    if (android.util.Patterns.EMAIL_ADDRESS.matcher(str).matches()) {
        return true;
    }
    toastMsg(_context.getString(R.string.please_enter_valid).toString()+" "+editName+ " "
            );
    return false;
}
like image 75
Yogendra Avatar answered Nov 20 '22 06:11

Yogendra


Both seems to be valid email addresses

[email protected]
[email protected]

since any email address contains three components

<username>@<mail-server>.<mail-servertype or server-location>

Here android.util.Patterns.EMAIL_ADDRESS validates all the three components using regex and also checks if all three components are placed properly. Some common confusing examples are:

  1. [email protected] is a valid mail address since com can be a user name and web123 can be a web server's name.

  2. [email protected] is also a valid mail address since .maths.apple can be a user name and com can be a web server's name.

Invalid case:

[email protected] is invalid since a . cannot come before or after @. No mailing system will be able to recognize the username or mail-server name.

like image 27
Prathmesh Swaroop Avatar answered Nov 20 '22 04:11

Prathmesh Swaroop


Well, the easiest way is to use the java Class InternetAddress instead of Android. Utils... or regex

like image 1
noah_abou Avatar answered Nov 20 '22 04:11

noah_abou