android.util.Patterns.EMAIL_ADDRESS is validating invalid emails

痴心易碎 提交于 2019-12-06 00:12:19

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 name of a webserver.

2.) .maths.apple@com.in also a valid mail address since .maths.apple can be a user name and com can be a name of a webserver.

Invalid case:

crick@.web.com is invalid since before and after @ if . is placed then any mailing system will never be able to recognize the username or mail-server name.

Yogendra

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;
}

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!