2015-06-21 181 views
0

對於我的學校考試,我們必須驗證電子郵件地址,但不允許使用RegularExpressionValidator。我正在嘗試用Custom Validator來解決這個問題,但我無法弄清楚。 (電子郵件輸入在文本框(tb_email)ASP.NET自定義驗證電子郵件

我試圖解決這個問題是這樣的:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) 
    { 
     if (tb_email.Text == \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*) 
     { 
      args.IsValid = true; 
     } 
     else 
     { 
      args.IsValid = false; 
     } 
    } 

或這樣的事情出現在腦海:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) 
    { 
     if (tb_email.Text != "") 
     { 
      string filter = \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*; 
      args.IsValid = true; 
     } 
     else 
     { 
      args.IsValid = false; 
     } 
    } 

我越來越龐大錯誤和我只是無法弄清楚如何做到這一點,我已經做了很多搜索,但我只能找到答案:只使用正則表達式驗證器或Java中的代碼解決方案(我還沒有學到) 我真的很感激一個很好的答案!謝謝

回答

0

你可以使用沒有REgularExpressionValidator的正則表達式嗎?如果這樣的話:

using System; 
using System.Text.RegularExpressions; 

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) 
{ 
    if (tb_email.Text != "") 
    { 
     string filter = \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*; 

     Regex regex = new Regex(filter); 
    Match match = regex.Match(tb_email.Text); 
    if (match.Success) 
    { 
     args.IsValid=true; } 
     else 
     { 
     args.IsValid = false; 

    } 
} 

在客戶端(JavaScript)看到這個帖子:

Validating email addresses using jQuery and regex