android.util.Patterns.EMAIL_ADDRESS is validating invalid emails

放肆的年华 提交于 2020-01-02 07:21:10

问题


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?


回答1:


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.




回答2:


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



回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/29492168/android-util-patterns-email-address-is-validating-invalid-emails

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