使用正则表达式验证电子邮

时间:2011-08-16 15:53:00

标签: java netbeans

我发现我可以使用正则表达式验证电子邮件输入。但是,我不知道我在哪里放置表达式。我把它们放在我的java控制器方法,实体类或JSP中吗?

4 个答案:

答案 0 :(得分:0)

如果您的电子邮件是字符串,您可能会写以下内容来验证电子邮件:

String email;
.
.  // load the email string
. 
if (email.matches("^[A-Za-z0-9_.]+[@][A-Za-z.]+$"))
{
.
.
.
}
else
    throw new EmailNotValidException();

答案 1 :(得分:0)

我认为您可以创建一些EmailValidatorClass类,只要您需要验证电子邮件地址,就可以在项目中使用该类:

import javax.mail.internet.InternetAddress;
import javax.mail.internet.AddressException;
import java.util.StringTokenizer;

/**
  * A class to provide stronger validation of email addresses.      
  *
  */
 public class EmailAddressValidator
 {
   public static boolean isValidEmailAddress(String emailAddress)
   {
     // a null string is invalid
     if ( emailAddress == null )
       return false;

     // a string without a "@" is an invalid email address
     if ( emailAddress.indexOf("@") < 0 )
       return false;

     // a string without a "."  is an invalid email address
     if ( emailAddress.indexOf(".") < 0 )
       return false;

     if ( lastEmailFieldTwoCharsOrMore(emailAddress) == false )
       return false;

     try
     {
       InternetAddress internetAddress = new InternetAddress(emailAddress);
       return true;
     }
     vcatch (AddressException ae)
     {
       // log exception
            return false;
     }
   }


   /**
    * Returns true if the last email field (i.e., the country code, or something
    * like .com, .biz, .cc, etc.) is two chars or more in length, which it really
    * must be to be legal.
    */
   private static boolean lastEmailFieldTwoCharsOrMore(String emailAddress)
   {
     if (emailAddress == null) return false;
     StringTokenizer st = new StringTokenizer(emailAddress,".");
     String lastToken = null;
     while ( st.hasMoreTokens() )
     {
       lastToken = st.nextToken();
     }

     if ( lastToken.length() >= 2 )
     {
       return true;
     }
     else
     {
       return false;
     }
   }
 }

答案 2 :(得分:0)

String expression = "[A-Za-z]+@+[A-Za-z]+\\.+[A-Za-z]{2,4}+$";
Pattern p = Pattern.compile(expression);
Matcher m = p.matcher(txtValidate.getText());
if(!m.matches())
{
JOpyionPane.showMessageDialog(null, "Email not valid");
}

答案 3 :(得分:0)

仅使用正则表达式无法创建正确的电子邮件验证。

以下是有关如何(几乎)正确验证电子邮件地址的说明。


根据RFC 5321RFC 5322验证电子邮件。以下是电子邮件语法的简化形式定义:

addr-spec = local-part "@" domain
local-part = dot-atom-text / quoted-string
dot-atom-text = atom *("." atom)
atom = 1*atext
atext = ALPHA / DIGIT / "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "/" / "=" / "?" / "^" / "_" / "`" / "{" / "|" / "}" / "~"
ALPHA = ASCII characters in the range ‘a’ (ACSII code 0x61) through ‘z’ (ACSII code 0x7A) and ‘A’ (ACSII code 0x41) through ‘Z’ (ACSII code 0x5A)
DIGIT = ASCII characters in the range ‘0’ (ACSII code 0x30) through ‘9’ (ACSII code 0x39)
specials = "(" / ")" / "<" / ">" / "[" / "]" / ":" / ";" / "@" / "\" / "," / "."
quoted-string = “ *(qcontent) “
qcontent = qtext / quoted-pair
qtext = 0x21 / 0x23-0x5B / 0x5D-0x7E; All ASCII printable characters instead of “ and \
quoted-pair = \ (VCHAR / WSP)
VCHAR = 0x21-0x7E; All ASCII printable characters
WSP = 0x20 / 0x09; ASCII Space or tab characters

除了这些规则,引用对可以放在 local-part 内的任何位置。 请注意,不使用特殊字符组。这些字符既不能出现在电子邮件的本地部分中,也不能出现在域部分中。 本地部分的长度最多为64个字符,域部分最多可以为255个字符(如果它应该是有效的DNS域名),只要整个电子邮件的总长度小于256个字符。

根据RFC 1034RFC 1123验证域名。以下是域名语法的正式定义:

domain = subdomain / " " 
subdomain = label / subdomain "." label
label = let-dig *(*(ldh-str) let-dig)
ldh-str = let-dig-hyp / let-dig-hyp ldh-str
let-dig-hyp = let-dig / "-"
let-dig = ALPHA / DIGIT

此外,RFC 1034限制为255个ASCII符号,将标签限制为63个ASCII符号。

Apache commons validation 库(在问题的评论中提到)是一个好的开始。它包含域验证和电子邮件验证,虽然我上次检查它时在电子邮件验证中有一些错误。