I came across article which explains how to do password strength validation.
I am having a problem with the errors I am encountering. One error states: Cannot implicitly convert type 'System.Text.RegularExpressions.Match' to 'bool' which is on line if (Regex.Match(password, @"/\d+/",...
The other error says: Operator '&&' cannot be applied to operands of type 'System.Text.RegularExpressions.Match' and 'System.Text.RegularExpressions.Match' which happens on line where AND or && statement is.
I don’t understand why Regex statement isn’t converting to bool type? And second problem is probably connected to the first one.
How can I fix this?
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;
}
You need to use
IsMatch, notMatch.IsMatchreturns abool, whileMatchreturns aMatchobject which gives you more detail (captured groups, etc)