5

These are few emails which are not valid.

email@domain.web
.email@domain.com

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?

victorkt
  • 13,992
  • 9
  • 52
  • 51
Nidhi
  • 148
  • 1
  • 1
  • 7
  • The first one certainly looks fine, [as `.web` is an unofficial TLD](http://en.wikipedia.org/wiki/.web). The second one may be an issue. – CommonsWare Apr 07 '15 at 13:03
  • Try this way hope this will help you, http://stackoverflow.com/questions/1819142/how-should-i-validate-an-e-mail-address – Silvans Solanki Apr 07 '15 at 13:05
  • 1
    The second email is invalid - Character . (dot, period, full stop) (ASCII: 46) provided that it is not the first or last character, and provided also that it does not appear consecutively (e.g. John..Doe@example.com is not allowed). Source: http://en.wikipedia.org/wiki/Email_address – Bojan Kseneman Apr 07 '15 at 13:34

4 Answers4

6

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;
}
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
Yogendra
  • 4,817
  • 1
  • 28
  • 21
4

Both seems to be valid email addresses

email@domain.web
.email@domain.com

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. com@web123.com is a valid mail address since com can be a user name and web123 can be a web server's name.

  2. .maths.apple@com.in 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:

crick@.web.com is invalid since a . cannot come before or after @. No mailing system will be able to recognize the username or mail-server name.

Michael
  • 8,362
  • 6
  • 61
  • 88
Prathmesh Swaroop
  • 611
  • 1
  • 5
  • 13
  • 2
    The local part ("user name" as you call it) cannot have a dot '.' at the start or the end of it unless it is in quotes. If Patterns.EMAIL_ADDRESS is saying `.email@domain.com` is valid, it is violating RFC rules: "period (".") may also appear, but may not be used to start or end the local part..." - https://tools.ietf.org/html/rfc3696 – Tom Aug 10 '16 at 21:23
  • android.util.Patterns.EMAIL_ADDRESS also accepts stuff like **test@t.t** which is definitely not a valid email address. Top level domain with only 1 letter is not possible. – Daniela Vladimirova Mar 13 '17 at 16:27
  • @DanielaVladimirova, One-letter TLDs are valid, they just don't exist yet. That could change at any time. See https://stackoverflow.com/questions/7411255/is-it-possible-to-have-one-single-character-top-level-domain-name – Samuel Bradshaw Sep 10 '18 at 21:00
1

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

noah_abou
  • 99
  • 12
-1

Use regex matching with this pattern below.

String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"


private Matcher matcher;
private Pattern pattern = Pattern.compile(EMAIL_PATTERN);


public boolean validateEmail(final String hex) {
    matcher = pattern.matcher(hex);
    return matcher.matches();
}

Hope this helps.

SripadRaj
  • 1,687
  • 2
  • 22
  • 33