【问题标题】:Validate textbox that it should contain one number and a special character验证文本框是否应包含一个数字和一个特殊字符
【发布时间】:2017-09-06 12:06:16
【问题描述】:

这是我的 C# 代码:

 private void btnLogin_Click(object sender, EventArgs e)
  {
     if (Regex.IsMatch(txtPassword.Text, @"(!|@|#)"))
      {
      MessageBox.Show("Password Must Contain at least a special character");
       }

       else
       {
     SqlConnection con = new SqlConnection("Data Source=Sumit;Initial Catalog=BroadDB;Integrated Security=True");
    string sql = "select * from tblLogin where username=@username and password=@password";
      SqlCommand cmd = new SqlCommand(sql, con);
      cmd.Parameters.AddWithValue("@username", txtUsername.Text);
      cmd.Parameters.AddWithValue("@password", txtPassword.Text);

   SqlDataAdapter da = new SqlDataAdapter(cmd);

    DataTable dt = new DataTable();
    da.Fill(dt);

    if (dt.Rows.Count > 0)
     {
      Form2 obj = new Form2();
      obj.Show();
     }

   else
    {
      MessageBox.Show("Invalid Data");
    }
  }        
    }

当点击登录按钮时,如何验证密码至少应包含一个数字和一个特殊字符。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    抱歉,我还不能发表评论,但你不是已经对特殊字符使用正则表达式了吗?

      if (Regex.IsMatch(txtPassword.Text, @"(!|@|#)"))
      {
      MessageBox.Show("Password Must Contain at least a special character");
      }
    

    如果我理解你的问题,你也可以用数字或字母来做。

    看看这个已回答的问题,它可以帮助你:

    How to check if a String contains any letter from a to z?

    更新

    这是因为你没有使用 -> !在 Regex.IsMatch 之前,因此 MessageBox 仅在用户输入特殊字符时出现,而不是在他不这样做时出现。

    试试这个:

      if (!Regex.IsMatch(txtPassword.Text, @"(!|@|#)"))
      {
      MessageBox.Show("Password Must Contain at least a special character");
      }
    

    【讨论】:

    • 即使没有输入特殊字符指向另一个表单并且没有显示消息框,该正则表达式代码也不起作用
    • 我不知道是不是这个问题,但是你有没有想过在密码错误的时候切断程序,也许用this.close()或者return?像这样的东西?到目前为止,您的 else 路径每次都会执行,是否因此而改变了形式?
    【解决方案2】:

    您可以通过测试从用户输入中检索到的字符串来实现这一点。为此,您最好使用regex

    Regex regexPassword = new Regex( @"
      ^               // From the start of the string
      [a-zA-Z0-9!@#]+ // The string should contain some of these characters, like letters including digits and special chars      
      (<=[!@#])       // one of these should be a special character
      (<=[0-9])       // one of these should be a number
      $               // end of string
    " , RegexOptions.IgnorePatternWhitespace ) ;
    

    那么你当然可以像往常一样针对你的字符串测试你的正则表达式:

    if(regexPassword.IsMatch(yourPasswordString))
    {
        //Do what you want
    }
    

    【讨论】:

      【解决方案3】:

      试试下面的代码

      if (!Regex.IsMatch(txtPassword.Text, @"(!|@|#)") || !txtPassword.Text.Any(char.IsDigit))
      {
          Console.WriteLine("Password Must Contain at least a special character and digit");
      }
      else
      {
          // DO YOUR STUFF
      }
      

      【讨论】:

        猜你喜欢
        • 2020-09-16
        • 1970-01-01
        • 2013-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 1970-01-01
        相关资源
        最近更新 更多