我遇到了 article这解释了如何进行密码强度验证。
我对遇到的错误有疑问。一个错误状态: Cannot implicitly convert type 'System.Text.RegularExpressions.Match' to 'bool'
which is on line if (Regex.Match(password, @"/\d+/",..
.
另一个错误是:Operator '&&' cannot be applied to operands of type 'System.Text.RegularExpressions.Match' and 'System.Text.RegularExpressions.Match'
这发生在行上AND
或 &&
语句是。
我不明白为什么 Regex 语句没有转换为 bool
类型?第二个问题可能与第一个问题有关。
我该如何解决这个问题?
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 >= 8)
score++;
if (password.Length >= 12)
score++;
if (Regex.Match(password, @"/\d+/", RegexOptions.ECMAScript))
score++;
if (Regex.Match(password, @"/[a-z]/", RegexOptions.ECMAScript) &&
Regex.Match(password, @"/[A-Z]/", RegexOptions.ECMAScript))
score++;
if (Regex.Match(password, @"/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/",
RegexOptions.ECMAScript))
score++;
return (PasswordScore)score;
}
最佳答案
您需要使用IsMatch
,而不是Match
。 IsMatch
返回一个 bool
,而 Match
返回一个 Match
对象,它为您提供更多详细信息(捕获的组等)
关于c# - 使用密码强度验证器和正则表达式时很少出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8393940/