如何验证android中的电子邮件?

时间:2017-05-30 11:18:03

标签: android

  

我的电子邮件验证有什么问题?

TextInputLayout useremail_layout = (TextInputLayout) findViewById(R.id.email_layout);
String email = "";
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
if (email.matches(emailPattern)&& email.length()>0) {
    useremail_layout.setError("valid address");
} else  {
     useremail_layout.setError("invalid email");
     Email.setError(null);  
}

2 个答案:

答案 0 :(得分:4)

使用以下代码:

public static boolean isValidEmail(String email) {
    return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}

答案 1 :(得分:0)

试试这个我的朋友

 String emailAddress = etSignInEmail.getText().toString().trim();

 if (android.util.Patterns.EMAIL_ADDRESS.matcher(emailAddress).matches()) {
     useremail_layout.setError("valid Email address");
}
else{
       useremail_layout.setError("invalid Email address");
}