【问题标题】:how to use Regex in password box in WPF如何在 WPF 的密码框中使用正则表达式
【发布时间】:2016-03-15 08:51:20
【问题描述】:

我想在 WPF 的密码框中测试我的输入密码,以便它唯一 aplhabets,数字和特殊字符..我做了以下代码bt循环直接进入Else Loop。我应该怎么做才能使其正常运行?

Regex regex = new Regex("^(?=.+[A-Za-z])(?=.+\\d)(?=.+[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,}$");
if (regex.IsMatch(txtPassword.ToString()))
{
    MessageBox.Show("Data Saved Correctly");   
}
else
{
    txtPassword.Clear();
    MessageBox.Show("Password should contain atleast one alphabet,number & special character");
}

【问题讨论】:

  • 显然你没有得到匹配 - 你的正则表达式没有按照你的想法做
  • txtPassword? 的类型是什么
  • 如果是TextBox 使用txtPassword.Text 而不是'txtPassword.ToString()`
  • txtPassword 是我在 WPF 中的密码框。显然你不能在密码框的情况下使用 txtpassword.Text。这就是我使用 .toString() 的原因
  • 嘿@SeM txtPassword 是一个密码框而不是文本框。没有 .text 可用于此。另外我目前不知道为什么正则表达式无法应用于此

标签: c# asp.net regex wpf passwordbox


【解决方案1】:

使用txtPassword.Password 而不是txtPassword.ToString()

祝你的项目好运:)

【讨论】:

    【解决方案2】:

    txtPassword.ToString() 给出了文本框的字符串表示形式..

    这应该是txtPassword.Password

    【讨论】:

    • 注意:使用SecurePassword 会是更好的选择,但我没有机会使用正则表达式检查来测试它。
    • @beginner_coder 不用担心,如果你有机会尝试使用安全密码,我链接的文档中有一条注释说明这是首选
    猜你喜欢
    • 2018-06-14
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 2010-11-18
    相关资源
    最近更新 更多