在 Java 中验证电子邮件

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

Validating email in Java

javajspspring-mvc

提问by user1277996

I have a registration form which has name, email and password. A confirmation link is sent to user's email address on registering. But before sending links I need to validate email address. I used:

我有一个注册表,上面有姓名、电子邮件和密码。确认链接会在注册时发送到用户的电子邮件地址。但在发送链接之前,我需要验证电子邮件地址。我用了:

public static boolean verifyEmailAddress(String regEmail) {
    boolean result = true;
    try {
        InternetAddress emailAddr = new InternetAddress(regEmail);
        emailAddr.validate();
    } catch (AddressException ex) {
        result = false;
    }
    return result;
}

Any of the email it shows valid, example [email protected], k999////@sdmail.com

它显示的任何电子邮件有效,例如[email protected]k999////@sdmail.com

The email I am getting from request.getParameterand storing it in regEmail.

我收到的电子邮件request.getParameter并将其存储在regEmail.

What exactly i need to do to make it work ?

我到底需要做什么才能让它发挥作用?

回答by Yomo710

OK heres about 3 examples http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/

好的,这里有 3 个例子 http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/

I think the one you need is

我认为你需要的是

package com.mkyong.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator{

  private Pattern pattern;
  private Matcher matcher;

  private static final String EMAIL_PATTERN = 
               "^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@
               [A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";

  public EmailValidator(){
      pattern = Pattern.compile(EMAIL_PATTERN);
  }

  /**
   * Validate hex with regular expression
   * @param hex hex for validation
   * @return true valid hex, false invalid hex
   */
  public boolean validate(final String hex){

      matcher = pattern.matcher(hex);
      return matcher.matches();

  }

}

}

Whole combination is means, email address must start with “_A-Za-z0-9-” , optional follow by “.[_A-Za-z0-9-]“, and end with a “@” symbol. The email's domain name must start with “A-Za-z0-9″, follow by first level Tld (.com, .net) “.[A-Za-z0-9]” and optional follow by a second level Tld (.com.au, .com.my) “\.[A-Za-z]{2,}”, where second level Tld must start with a dot “.” and length must equal or more than 2 characters.

整个组合意味着,电子邮件地址必须以“_A-Za-z0-9-”开头,可选后跟“.[_A-Za-z0-9-]”,并以“@”符号结尾。电子邮件的域名必须以“A-Za-z0-9”开头,后跟一级Tld(.com,.net)“.[A-Za-z0-9]”,可选后跟二级Tld( .com.au, .com.my) “\.[A-Za-z]{2,}”,其中二级 Tld 必须以点“.”开头。并且长度必须等于或大于 2 个字符。

Example

例子

package com.mkyong.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator{

  private Pattern pattern;
  private Matcher matcher;

  private static final String EMAIL_PATTERN = 
               "^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@
               [A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";

  public EmailValidator(){
      pattern = Pattern.compile(EMAIL_PATTERN);
  }

  /**
   * Validate hex with regular expression
   * @param hex hex for validation
   * @return true valid hex, false invalid hex
   */
  public boolean validate(final String hex){

      matcher = pattern.matcher(hex);
      return matcher.matches();

  }

}

}

回答by jordeu

Use Apache Commons Validator. Check the EmailValidator

使用Apache Commons 验证器。检查电子邮件验证器

You can use it like this example:

你可以像这个例子一样使用它:

 EmailValidator validator = EmailValidator.getInstance();
 boolean validEmail = validator.isValid(email);

回答by H Marcelo Morales

There are a number of email validators in the public domain.

公共领域中有许多电子邮件验证器。

As mentioned, the email validator from apache commonsis very much in use.

如前所述,来自 apache commons电子邮件验证器正在大量使用。

There is a very interesting one on the apache wicket codebase, which apparently is quite good as it is a 81line regular expression. Just for fun, I will copy and paste the whole thing.

apache wicket 代码库中有一个非常有趣的代码,它显然非常好,因为它是一个81行的正则表达式。只是为了好玩,我将复制并粘贴整个内容。

private static final String EMAIL_PATTERN = "(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]"
    + ")+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:"
    + "\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:("
    + "?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ "
    + "\t]))*\"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\0"
    + "31]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\"
    + "](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+"
    + "(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:"
    + "(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z"
    + "|(?=[\[\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)"
    + "?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\"
    + "r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?["
    + " \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)"
    + "?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t]"
    + ")*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?["
    + " \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*"
    + ")(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t]"
    + ")+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)"
    + "*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+"
    + "|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r"
    + "\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:"
    + "\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t"
    + "]))*\"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031"
    + "]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\]("
    + "?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?"
    + ":(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?"
    + ":\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)|(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?"
    + ":(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?"
    + "[ \t]))*\"(?:(?:\r\n)?[ \t])*)*:(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:\\".\[\] "
    + "\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|"
    + "\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>"
    + "@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\""
    + "(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t]"
    + ")*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\"
    + "\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?"
    + ":[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\["
    + "\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\".\[\] \000-"
    + "\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|\\.|("
    + "?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;"
    + ":\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[(["
    + "^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\""
    + ".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\"
    + "]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\"
    + "[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\"
    + "r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] "
    + "\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]"
    + "|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\".\[\] \0"
    + "00-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|\\"
    + ".|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,"
    + ";:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\"(?"
    + ":[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*"
    + "(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\"."
    + "\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:["
    + "^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\]"
    + "]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*("
    + "?:(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\"
    + "\".\[\]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)?[ \t])*)(?:\.(?:("
    + "?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=["
    + "\[\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)?[ \t"
    + "])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t"
    + "])+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?"
    + ":\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|"
    + "\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:"
    + "[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\".\[\"
    + "]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)"
    + "?[ \t])*(?:@(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\""
    + "()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)"
    + "?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>"
    + "@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?["
    + " \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,"
    + ";:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t]"
    + ")*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\"
    + "\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?"
    + "(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\[\"()<>@,;:\\"."
    + "\[\]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:"
    + "\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["
    + "\"()<>@,;:\\".\[\]]))|\"(?:[^\\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*\"(?:(?:\r\n)?[ \t])"
    + "*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])"
    + "+|\Z|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\"
    + ".(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z"
    + "|(?=[\[\"()<>@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:("
    + "?:\r\n)?[ \t])*))*)?;\s*)";

回答by iperezmel78

Try this:

试试这个:

public boolean isEmail(String s) {
    return s.matches("^[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}");
}

If you need email to have certain length before the @ character. For instance 5:

如果您需要电子邮件在@ 字符之前有一定的长度。例如5:

s.matches("^[-0-9a-zA-Z.+_]{5}+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}")

The {5} in expression validates length.

表达式中的 {5} 验证长度。