Java Android 检查电子邮件地址是否有效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22348212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:06:40  来源:igfitidea点击:

Android check if an email address is valid or not?

javaandroid

提问by mohammad rababah

I develop an android app that can send emails to users.
But it turned out that some email addresses are not valid.
How check if the email address entered by the user is valid or not?

我开发了一个可以向用户发送电子邮件的 android 应用程序。
但事实证明,某些电子邮件地址无效。
如何检查用户输入的电子邮件地址是否有效?

回答by Renjith Krishnan

There is no way of checking email is real or not. But You can check only the validation that is it in correct format or not.

没有办法检查电子邮件是否真实。但是您只能检查格式是否正确的验证。

回答by Juan Cortés

Probably the best way to check if the email is real, is to actually send an email to that address with a verification code/link that will activate that user on your site. Using regular expressions will only make sure the email is valid, but not necessarily real.

检查电子邮件是否真实的最佳方法可能是实际向该地址发送一封带有验证码/链接的电子邮件,该验证码/链接将在您的网站上激活该用户。使用正则表达式只会确保电子邮件有效,但不一定是真实的

回答by Shayan Pourvatan

if you want check validate of email you can use:

如果您想检查电子邮件的验证,您可以使用:

public static boolean isValidEmail(CharSequence target) {
    if (target == null) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}

but there is no way to find out what email address is real

但没有办法找出哪个电子邮件地址是真实的

you can use following method for removing if/else.

您可以使用以下方法删除 if/else。

public static boolean isValidEmail(CharSequence target) {
       return target != null && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }

回答by Theolodis

There is no way to know if an email exists or not. Specially if it is on a site like yopmailor similar, which I believe would accept any mail to any account on their domain.

没有办法知道电子邮件是否存在。特别是如果它在像yopmail或类似的网站上,我相信它会接受任何邮件到他们域上的任何帐户。

However, you can check:

但是,您可以检查:

1. if the address has the correct syntax and is on a registered domain (regexfor syntax and a dnscheck if there is a mailserver for the site behind the @)

1. 如果地址具有正确的语法并且在注册域中(语法的正则表达式dns检查后面的站点是否有邮件服务器@

2. send an e-mail and check the response, some providers might send you back an error if the mail is not registered on their site.

2. 发送电子邮件并检查响应,如果邮件未在其站点上注册,某些提供商可能会向您发送错误消息。

回答by jyomin

Use below code:

使用以下代码:

if(validateEmail(mEdtTxtEmail.getText().toString().trim())){
            // your code        
                }

private boolean validateEmail(String data){
        Pattern emailPattern = Pattern.compile(".+@.+\.[a-z]+");
        Matcher emailMatcher = emailPattern.matcher(data);
        return emailMatcher.matches();
    }

回答by Phant?maxx

You can use a Regular expression to validate an email address, so:

您可以使用正则表达式来验证电子邮件地址,因此:

public boolean isEmailValid(String email)
{
    final String EMAIL_PATTERN =
        "^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";
    final Pattern pattern = Pattern.compile(EMAIL_PATTERN);
    final Matcher matcher = pattern.matcher(email);
    return matcher.matches();
}

Hereis a link with more RegExes to choose from.

是一个包含更多 RegEx 可供选择的链接。

回答by CoderDecoder

This code works totally fine and it gives you a boolean value. So if its true it will give true and false it will give you false

这段代码工作得很好,它给你一个布尔值。所以如果它是真的,它会给真假,它会给你假的

         android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();

回答by W4R10CK

Use :

用 :

if (!Patterns.EMAIL_ADDRESS.matcher(your_edit_text.getText().toString()).matches()){
    loginEmail.setError("Please enter a Valid E-Mail Address!");
}else {
    //email is valid
}

回答by ChrisPrime

Here is a Kotlin version using Kotlin Extensions (extending the String object, so you can call stringName.isValidEmail():

这是使用 Kotlin 扩展的 Kotlin 版本(扩展 String 对象,因此您可以调用stringName.isValidEmail()

fun String.isValidEmail(): Boolean { return android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches() }

fun String.isValidEmail(): Boolean { return android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches() }