2012-08-02 23 views
-1

我使用這個正則表達式來檢查電子郵件。 /^\w+([-+.'']\w+)*@\w*([-.çöişğü]\w*)*\.\w+([-.]\w+)*$/ 但是,這個正則表達式不接受像[email protected]電子郵件。你能幫我改變我的正則表達式來接受像這封電子郵件嗎?電子郵件正則表達式 - 字符

+0

爲什麼土耳其字符'çöişğü'的特例?要麼你想支持完整的IDNA,要麼你堅持使用ASCII/punycode。 – tripleee 2012-08-02 07:29:29

回答

0

更改第二\w+\w*

^\w+([-+.'']\w*)*@\w*([-.çöişğü]\w*)*\.\w+([-.]\w+)*$
+0

非常感謝你^ \ w +([ - +。''] \ w *)* @ \ w *([ - 。çöişğü] \ w *)* \。\ w +([ - 。] \ w +)* $它幫助了我。 – 2012-08-02 08:22:44

+0

我更改差異粗體。解決的問題? – Ria 2012-08-02 08:26:08

0

我用這個在HTML5模式 電子郵件模式:

^(?:[-+~=!#$%&'*/?\^`{|}\w]+)(?:\.[-+~=!#$%&'*/?\^`{|}\w]+)*@(?:[a-zA-Z0-9][-a-zA-Z0-9]*[a-zA-Z0-9]\.)+[a-zA-Z]{2,6}$ 
0
/^\w+([-+.'']*\w*)*@\w*([-.çöişğü]\w*)*\.\w+([-.]\w+)*$/ 
1

如果用c#標籤標記你的問題你爲什麼不使用內建的System.Net.Mail.MailAddress類構造函數進行電子郵件驗證?它支持許多郵件地址格式,並且涵蓋比正則表達式更多的場景。請參閱:

var isEmailValid = false; 
try 
{ 
    var email = new MailAddress("[email protected]"); 
    isValidEmail = true; 
{ 
catch (FormatException x) 
{ 
    // invalid email address 
} 

難道不比您的正則表達式容易嗎?

0

這是我特別用於電子郵件驗證的正則表達式,但我不會以這種方式推測,特別是在商業環境中使用時;由於這個事實,當考慮電子郵件驗證正則表達式太嚴格。它們不允許有可能完成的領域的空間,並且包含所有這些可能性會使正則表達式變得漫長,使其不穩定。

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

    Regex re = new Regex(strRegex); 
    if (re.IsMatch(txtEmail.Text) || txtEmail.Text == "" || txtEmail.Text.Length > 100 && txtEmail.Text.Length < 10) 
    { 
     MessageBox.Show("Thanks"); 

    } 

    else 
    { 
     MessageBox.Show("Please enter a valid email address"); 



    } 

} 

這是solotuion我建議這是更加穩定和高效能utiliseszsthe EmailAddressAttribute,因爲它是一個內置的類和允許MROE房間,而不是嚴格的正則表達式。希望這可以幫助。

 TextBox tb = new TextBox(); 
     tb.KeyDown += new KeyEventHandler(txtEmail_KeyDown); 
     // Run Checks after the enter is pressed. 
     if (e.KeyCode == (Keys.Enter) || e.KeyCode == (Keys.Tab)) 
     { 
      if (!new EmailAddressAttribute().IsValid(txtEmail.Text)) 
      { 

       MessageBox.Show(txtEmail.ToString() + " is not a valid Email address"); 
       txtEmail.Clear(); 
      } 
      else 
      { 
       MessageBox.Show("The address: " + txtEmail + " is valid"); 
       txtEmail.Clear(); 
      } 
     } 
相關問題