【问题标题】:How to validate textbox not to accept emailids如何验证文本框不接受电子邮件ID
【发布时间】:2018-11-21 09:37:44
【问题描述】:

我有一个字段Company profile: 文本框

如果用户在文本框中输入任何 emailid,验证错误消息应显示用户无法在文本框中输入 emailid。

我已经尝试了以下代码:

Regex regex = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");

string[] values = commentstxt.Text.Trim().Split(' ');
for (int i = 0; i < values.Length; i++)
{
    bool isValid = regex.IsMatch(values[i].ToString().Trim());
    if (isValid)
    {
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
        //break;
        Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
        break;
    }
    else
    {
        Server.Transfer("addlistingpost.aspx", true);
    }
}

如果用户只输入test@gmail.com,它会给出验证消息说你不能在文本框中输入正确的emailid,并停留在addlisting.aspx页面中。

如果用户输入hello..how are you,它会重定向到addlistingpost.aspx,这也是正确的。

当用户输入 hello test@gmail.com how are you 时出现问题,它不会抛出验证消息,因为文本框中存在 emailid。我知道这里只是比较values[0],也就是hello,然后直接进入else部分。

如何做到这一点?

【问题讨论】:

  • 什么是“emailids”?
  • 另外,您使用的正则表达式与合法电子邮件地址的 RFC 5322 规范不匹配。

标签: c# asp.net validation


【解决方案1】:

您使用的正则表达式匹配字符串的开头 (^) 和结尾 ($)。

^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$

只需删除这些字符以匹配该行中的任何位置。

([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)

试试这个代码:

Regex regex = new Regex(@"([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)");
string text = "hello test@gmail.com how are you";
Console.WriteLine(regex.IsMatch(text));

它输出True

顺便说一下,这是一个几乎符合 RFC 5322 规范的正则表达式:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

【讨论】:

    【解决方案2】:

    您需要扫描所有阵列,直到发现任何错误。种

    Regex regex = new Regex(    @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
    string[] values = commentstxt.Text.Trim().Split(' ');
    bool isValid = true; // valid word == not email
    for (int i = 0; i < values.Length && isValid; i++)
    {
        bool isValid = !regex.IsMatch(values[i].ToString().Trim());
        if (!isValid)
        {
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
    
            Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
    
        }
    }
    if (isValid)
    {
        Server.Transfer("addlistingpost.aspx", true);
    }
    

    【讨论】:

    • 嗨@Serg ...我是否可以在Regex中使用逻辑或部分,这样它也不接受网址,即www。
    【解决方案3】:
    Regex regex = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            string[] values = commentstxt.Text.Trim().Split(' ');
    
            bool isValid = false;
    
    
            for (int i = 0; i < values.Length; i++)
            {
    
                isValid = regex.IsMatch(values[i].ToString().Trim());
    
    
                if (isValid)
                {
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
                    //break;
                    Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
                    break;
                }
                else
                {
                    continue;
    
                }
    
    
            }
    
            if(!isValid)
            {
                Server.Transfer("addlistingpost.aspx", true);
            }
    

    【讨论】:

      【解决方案4】:

      你能试试这样的方法,看看是否可行? :

      Regex regex = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
          string[] values = commentstxt.Text.Trim().Split(' ');
          bool hasEmail = false;
          foreach (string str in values)
          {
              bool isCurrentValid = regex.IsMatch(str.Trim());
              if (!isValid)
              {
                  hasEmail = false;
              } else {
                  hasEmail = true;
                  break;
              }
          }
      
          if(hasEmail) {
              Server.Transfer("addlistingpost.aspx", true);
          } 
          else 
          { 
              Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
          }  
      

      我基本上将检查整个字符串的逻辑排除在循环之外。

      【讨论】:

        猜你喜欢
        • 2013-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-10
        • 2016-06-24
        • 1970-01-01
        • 2013-02-11
        • 1970-01-01
        相关资源
        最近更新 更多