在检查密码强度验证器中的字符时,Regex似乎不能识别和计数

本文关键字:Regex 不能 识别 字符 密码 检查 验证 | 更新日期: 2023-09-27 18:16:48

我将使用这篇解释如何使用密码强度验证器的文章。

问题是,在输入密码时,它似乎没有检查和计算每一步。似乎它只检查超过6个字符和超过10个字符,给我的最大计数为3。

问题可能是因为我使用TextChanged功能?

下面是验证器的代码:

enum PasswordScore
{
    Blank = 0,
    VeryWeak = 1,
    Weak = 2,
    Medium = 3,
    Strong = 4,
    VeryStrong = 5
}
private static PasswordScore CheckStrength(string password)
{
    int score = 1;
        if (password.Length < 1)
            return PasswordScore.Blank;
        if (password.Length < 4)
            return PasswordScore.VeryWeak;
        if (password.Length >= 6)
        score++;
        if (password.Length >= 10)
            score++;
        if (Regex.IsMatch(password, @"/'d+/", RegexOptions.ECMAScript))
            score++;
    if (Regex.IsMatch(password, @"/[a-z]/", RegexOptions.ECMAScript) &&
        Regex.IsMatch(password, @"/[A-Z]/", RegexOptions.ECMAScript))
            score++;
    if (Regex.IsMatch(password, @"/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/",
        RegexOptions.ECMAScript))
            score++;
        return (PasswordScore)score;
}

这是我的TextChanged函数(注意:我传递一个值给检查方法的代码是在这段代码的第二到最后一行):

    // Checking user input for variety of things.
    // This is intended as security measure.
    private void validateInput(object sender, EventArgs e)
    {
        // ======== Start validating Password field ========
        // Checking for Null or Empty string in password field.
        if (string.IsNullOrEmpty(txtPassword.Text))
        {
            lblMessagePass.Text = "Password field cannot be empty!";
            lblMessagePass.ForeColor = Color.IndianRed;
            btnAuthenticate.Enabled = false;
            passIsValid = false;
        }
        // Making sure that user name is at least 6 characters long.
        else if (txtPassword.Text.Length < 6)
        {
            lblMessagePass.Text = "Password field must be at least 6 characters long!";
            lblMessagePass.ForeColor = Color.IndianRed;
            btnAuthenticate.Enabled = false;
            passIsValid = false;
        }
        // Checking for password made of same repeating character.
        // Invalid input example: 'aaaaaa'
        else if (!txtPassword.Text.Distinct().Skip(1).Any())
        {
            lblMessagePass.Text = "Password cannot be made of repeating the same characters!";
            lblMessagePass.ForeColor = Color.IndianRed;
            btnAuthenticate.Enabled = false;
            passIsValid = false;
        }
        // Making sure that user name and password are not the same.
        // Security measure.
        else if (txtUserName.Text == txtPassword.Text)
        {
            lblMessagePass.Text = "User Name and Password can not be the same!";
            lblMessagePass.ForeColor = Color.IndianRed;
            btnAuthenticate.Enabled = false;
            passIsValid = false;
        }
        // If all other checks aren't trigered; enable authentication.
        else
        {
            lblMessagePass.Text = "Password is valid.";
            lblMessagePass.ForeColor = Color.Green;
            passIsValid = true;
            if (passIsValid && userIsValid)
            {
                btnAuthenticate.Enabled = true;
            }
        }
        // ======== End validating Password field ========
        lblStrength.Text = CheckStrength(txtPassword.Text).ToString();
    }

在检查密码强度验证器中的字符时,Regex似乎不能识别和计数

您不需要在Regex模式中使用斜杠(/),或者在最后一个模式中使用逗号。
试试这个:

if (Regex.IsMatch(password, @"'d+", RegexOptions.ECMAScript))
    score++;
if (Regex.IsMatch(password, @"[a-z]", RegexOptions.ECMAScript) &&
    Regex.IsMatch(password, @"[A-Z]", RegexOptions.ECMAScript))
    score++;
if (Regex.IsMatch(password, @".[!@#'$%'^&'*'?_~'-£'(')]", RegexOptions.ECMAScript))
    score++;