gpt4 book ai didi

c# - 在密码强度验证器中检查字符时,正则表达式似乎无法识别和计数

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:13 34 4
gpt4 key购买 nike

我会用这个 article其中解释了如何使用密码强度验证器。

问题是它在输入密码时似乎没有检查和计算每一步。它似乎只检查超过 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();
}

最佳答案

您不需要正则表达式模式中的斜杠 (/),也不需要最后一个中的逗号。
试试这个:

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++;

关于c# - 在密码强度验证器中检查字符时,正则表达式似乎无法识别和计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8394191/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com