Java 字符串中电子邮件的非混淆简单验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5681808/
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-10-30 12:16:28  来源:igfitidea点击:

Non-Confusing Simple Validation of email in Java String

javastringemail-validation

提问by Michael

I am trying to find a simple method to check to see if a user's input meets a couple criteria for an email address. I've read through many threads on this subject and most seem to want to validate the email address too. I'm not trying to build some super duper email address validator/checker. I'm trying to build a method that checks for these things:

我试图找到一种简单的方法来检查用户的输入是否满足电子邮件地址的几个标准。我已经阅读了许多关于这个主题的主题,大多数人似乎也想验证电子邮件地址。我不是要构建一些超级欺骗电子邮件地址验证器/检查器。我正在尝试构建一个检查这些事情的方法:

  1. The string entered by the user contains the '@' sign.
  2. There are at least two characters before the '@' sign.
  3. There is a '.' after the at sign followed by only three characters. The domain name can be as long as needed, but the string must end with "._ _ _". As in ".com" or ".net"...
  1. 用户输入的字符串包含“@”符号。
  2. '@' 符号前至少有两个字符。
  3. 有一个'.' 在 at 符号后跟仅三个字符。域名可以任意长,但字符串必须以“._ _ _”结尾。如在“.com”或“.net”中...

I understand that this is not an all encompassing email address checker. That's not what I want though. I want just something this simple. I know that this is probably a routine question but I can't figure it out even after reading all of the seriously crazy ways of validating an email address.

我知道这不是一个包罗万象的电子邮件地址检查器。不过那不是我想要的。我只想要这么简单的东西。我知道这可能是一个例行问题,但即使在阅读了所有验证电子邮件地址的非常疯狂的方法后,我也无法弄清楚。

This is the code I have so far: (Don't worry I already know it's pretty pathetic.... )

这是我到目前为止的代码:(别担心,我已经知道它很可悲......)

public static void checkEmail()
{
    validEmail(emailAddresses);
    if(validEmail(emailAddresses))
    {

    }
}

public static boolean validEmail(String email) {
    return email.matches("[A-Z0-9._%+-][A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{3}");
}

回答by Daniel DiPaolo

The javax.mailpackage provides a class just for this: InternetAddress. Use this constructorwhich allows you to enforce RFC822 compliance.

javax.mail包为此提供了一个类: InternetAddress。使用此构造函数可让您强制执行 RFC822 合规性。

回答by corsiKa

Not perfect, but gets the job done.

不完美,但可以完成工作。

static boolean validEmail(String email) {
    // editing to make requirements listed
    // return email.matches("[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}");
    return email.matches("[A-Z0-9._%+-][A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{3}");
}

void checkEmails() {
    for(String email : emailAddresses) {
        if(validEmail(email)) {
             // it's a good email - do something good with it
        }
        else {
             // it's a bad email - do something... bad to it? sounds dirty...
        }
    }
}

回答by JB Nizet

int indexOfAt = email.indexOf('@');
// first check : 
if (indexOfAt < 0) { 
  // error
}

// second check :
if (indexOfAt < 2) {
  // error
}

// third check :
int indexOfLastDot = email.lastIndexOf('.');
if (indexOfLastDot < indexOfAt || indexOfLastDot != (email.length() - 4)) {
  // error
}

Read http://download.oracle.com/javase/6/docs/api/java/lang/String.htmlfor the documentation of the String methods.

有关 String 方法的文档,请阅读http://download.oracle.com/javase/6/docs/api/java/lang/String.html