2011-09-26 57 views
1

我有休耕電子郵件ID非英語(UTF-8)字符驗證電子郵件地址其中包含的Java

閃閃發光@閃閃發光.com 

我需要在服務器端驗證這種類型的電子郵件,使用戶無法進入該類型的電子郵件..
我以低於regex-

/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/gi 

但解決在JavaScript中類似的問題。無法在java.lang中做同樣的事情。請幫助我們。
在此先感謝!

+1

問題是什麼? Java正則表達式模式和匹配器應該做... – Gandalf

+0

您確定要排除UTF域名嗎? – Matteo

+0

是的,我知道但我沒有得到如何執行此.. – Vivek

回答

1

Java的正則表達式模式(?i)[-a-z0-9+_][-a-z0-9+_.]*@[-a-z0-9][-a-z0-9.]*\\.[a-z]{2,6}應該足夠了。下面是該模式是指:

(?i)   # Case insensitive flag 
[-a-z0-9+_]  # First character 
[-a-z0-9+_.]* # Zero or more characters 
@    # Literal '@' character 
[-a-z0-9]  # Match a single character 
[-a-z0-9.]*  # Match zero or more characters 
\.    # Literal '.' character 
[a-z]{2,6}  # Match 2 through 6 alpha characters 

下面的測試代碼...

final String ps = 
     "(?i)[-a-z0-9+_][-a-z0-9+_.]*@[-a-z0-9][-a-z0-9.]*\\.[a-z]{2,6}"; 
final Pattern p = Pattern.compile(ps); 
for (String s : new String[] {"[email protected]", "[email protected]", 
     "[email protected]", "[email protected]", "[email protected]", "[email protected]", 
     "[email protected]", "[email protected]", "[email protected]", "[email protected]", 
     "閃閃發光@閃閃發光.com"}) 
{ 
    final Matcher m = p.matcher(s); 
    if (m.matches()) { 
     System.out.println("Success: " + s); 
    } else { 
     System.out.println("Fail: " + s); 
    } 
} 

... ...將輸出:

Success: [email protected] 
Success: [email protected] 
Success: [email protected] 
Success: [email protected] 
Success: [email protected] 
Success: [email protected] 
Success: [email protected] 
Success: [email protected] 
Fail: [email protected] 
Fail: [email protected] 
Fail: 閃閃發光@閃閃發光.com 

通過使用Matcher.matches()方法,你不需要包括^開始行或$行結束邊界匹配結構,因爲Matcher.matches()將匹配整個字符串。

+0

爲什麼你想要這個「失敗:[email protected]」失敗?這不能說是一個完全有效的內部電子郵件地址,如:[email protected](我正在使用的機器; D) –

+0

@Angel O'Sphere:OP將頂級域名限制爲2到6個字符;超過6個字符應該失敗。我認爲這是因爲這[頂級域名列表](https://secure.wikimedia.org/wikipedia/en/wiki/List_of_Internet_top-level_domains)中最長的頂級域名是'博物館'...... 6個字符。 –

+0

@丹克魯斯 - 謝謝你的解釋性答案。如果我不想限制頂級域名到6那麼我需要這樣做 - '(?i)[ - a-z0-9 + _] [ - a- z0-9 + _。] * @ [ - a-z0-9] [ - a-z0-9。] * \\。[az] *'或其他。 – Vivek

0

[更新]遺憾的是js代碼。試試這個:

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(); 

      } 
    } 
+0

對我來說看起來不像Java-Code!? – Gandalf

+0

這是在JavaScript的人..我想在Java中做.. – Vivek

+0

@Vivek:我編輯帖子。請嘗試新的解決方案。 –