2012-11-06 32 views
1

我有一些錯誤,當用戶無法在網站上註冊。 JS jquery驗證器通過,但在控制器(asp)的一側 - 不正確驗證。正則表達式電子郵件錯誤

請告訴我,是否有可能獲得此類型的用戶電子郵件以及如何驗證此電子郵件(按照表達方式)?

可驗證的電子郵件: 「[email protected]

C#電子郵件模式正則表達式

const string email_pattern = 
     @"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9][email protected]((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$"; 

    const string email_pattern3 = 
     @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; 

    const string email_pattern2 = 
     @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$"; 

    const string email_pattern3 = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@" 
+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]? 
      [0-9]{1,2}|25[0-5]|2[0-4][0-9])\." 
+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]? 
      [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" 
+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$"; 
+3

人們_still_問這個問題?認真。只需使用'try {new MailAddress(email);如果它失敗了,那很糟糕。除非你打算使用這些電子郵件來處理任何東西,而是使用默認的.NET類。 –

+0

我正在做System.Net.Mail MailAddress,但我想改爲正則表達式。 –

+1

@YauhenKavalenka plzz..dont use'regex'....this like'cutting' a tree with ** hammer ** – Anirudha

回答

4

如果您想驗證一個郵箱地址正則表達式是不是正確的選擇。

使用MailAddress建議由我們自己怪胎SLaks

try 
{ 
    address = new MailAddress(address).Address; 
    //address is valid id reached here 
} 
catch(FormatException) 
{ 
    //address is invalid 
} 

但是,如果你沉迷於regex..just做到這一點

.*[email protected]* 

[如果你想要完整的正則表達式]

正如SLaks

(?:(?:\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)?[ \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0

我明白自己的錯誤,並且真正吸取了人生的教訓。 我感謝所有幫助我認識到錯誤的人。 最好的問候,亞琛。 –

3

哦,我的上帝提供。人!我討厭Net.Mail。正則表達式是最好的。

看看這段代碼,你會發現我是對的。

public const string Email4 = 
     @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"; 

    private static Regex regex; 

    private static void Main(string[] args) 
    { 
     var regex = new Regex(Email4, RegexOptions.Compiled); 

     var list = new List<string>(); 
     var str = new[] 
         { 
          // Success email 
          "[email protected]", 
          "[email protected]", "[email protected]", 
          "[email protected]", "[email protected]", 
          "[email protected]", "[email protected]", 
          "[email protected]", "[email protected]", 
          "[email protected]", 
          //Fail email 
          "mkyong", "[email protected]", 
          "[email protected]", "[email protected]", "[email protected]", 
          "[email protected]", "mkyong()*@gmail.com", "[email protected]%*.com", 
          "[email protected]", "[email protected]", 
          "[email protected]@gmail.com", "[email protected]", 
          // Test Email project 
          "[email protected]", // success 
          "[email protected]", // success 
          "[email protected]", // fail 
          // success 
          "[email protected]" 
         }; 
     list = str.ToList(); 
     foreach (string x in list) 
     { 
      foreach (string st in ParseEmail(x)) 
      { 
       Console.WriteLine(st); 
      } 
     } 
     Console.ReadKey(); 
    } 

    private static IEnumerable<string> ParseEmail(string str) 
    { 
     regex = new Regex(email_pattern, RegexOptions.Compiled); 
     return regex.Matches(str).Count > 0 
        ? (from x in regex.Matches(str).Cast<Match>() 
         select (x.Value + "\t\t True\t" + Validate(str))) 
        : new[] {str + "\t\t False\t"+Validate(str)}; 
    } 

    private static bool Validate(string email) 
    { 
     try 
     { 
      var address = new MailAddress(email); 
      if (address.Address == email || address.Address.Equals(email)) 
       return true; 
      else 
       return false; 
     } 
     catch (Exception exception) { return false; } 
    } 

> plzz..dont使用regex..this是等切斷樹與錘 - Fake.It.Til.U.Make.It

現在,所有一起上的圖案並看看圖書館。事實證明,Net.Mail錯過了比模式正則表達式更糟糕的錯誤。

如果有人遇到這種情況,不需要任何其他的東西,在這裏我寫了自己。

@"^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9_][email protected]((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$"; 

對於那些誰尋求不同的解決方案 Email Regex compare

相關問題