2011-08-16 52 views
0

我發現我可以使用正則表達式驗證電子郵件輸入。但是,我不知道我在哪裏放置表達式。我把它們放在我的java控制器方法,實體類或JSP中嗎?使用正則表達式的電子郵件驗證

+0

你爲什麼不使用commons驗證器? – Artefacto

+1

請參考以下問題,只是爲了給出一些比例:http://articleover.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses – MByD

+0

@Artefacto commons validator ?這是一個進口還是?我是一個java新手>< – gymcode

回答

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; 
    } 
    } 
} 
0

如果您的電子郵件是在一個字符串,你可能會寫類似下面的驗證電子郵件:

String email; 
. 
. // load the email string 
. 
if (email.matches("^[A-Za-z0-9_.]+[@][A-Za-z.]+$")) 
{ 
. 
. 
. 
} 
else 
    throw new EmailNotValidException(); 
0
String expression = "[A-Za-z][email protected]+[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"); 
} 
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 

除了這些規則的報價,對可以放在任何地方的本地部分內。 請注意,不使用特殊字符組。這些字符既不能出現在電子郵件的本地部分也不出現在域部分中。 本地部分的長度可以達到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共享驗證庫(在問題的評論中提到)是一個好的開始。它包含域驗證和電子郵件驗證,儘管上次我檢查過它在電子郵件驗證中有一些錯誤。

相關問題