Password Regex Help
-
Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim
-
Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim
RegEx is not a good fit for password rules. You'd be much better off coding each rule separately.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim
The problem with using a regex for this is that it gets very, very complex - and that means that when the rules change (and they always do) a horribly complicated and difficult to understand string has to be modified, tested, fixed, tested, and finally released. Which makes maintenance difficult and prone to error. Instead, use .NET string operations (or individual Regexes) to pick up each individual part:
int numLoCase = ...
int numUpCase = ...
int numSpaces = ...
int numNumeric = ...
int numSpecial = ...
int len = ...And then apply a single
if
statement to apply the rules:if (len >= 10
&& len <= 50
&& numSpaces == 0
&& ...You get much more readable code, and more reliable maintenence.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim
Jassim Rahma wrote:
Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~
Tell management to fuck off. A horse staple is better. Let's do a Zoom where I can explain that fine detail. I will be recording for training purposes only.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim
probe the next function:
bool IsValidPassword(string password,
int minLength=10,
int maxLength=50,
bool requireLowercase=true,
bool requireUppercase=false,
bool requireNumber=false,
bool requireSimbol=false)
{
if (string.IsNullOrEmpty(password))
{
return false;
}
if (password.Length < minLength || password.Length > maxLength)
{
return false;
}
if (requireUppercase && !Regex.IsMatch(password, "[A-Z]"))
{
return false;
}
if (requireLowercase && !Regex.IsMatch(password, "[a-z]"))
{
return false;
}
if (requireNumber && !Regex.IsMatch(password, "[0-9]"))
{
return false;
}
string pattern= @"[!@#$%^&*()_+\-=\[\]{};':" + "\"" + @"\\|,.<>\/? ~]";
if (requireSimbol && !Regex.IsMatch(password, pattern))
return false;
return true;
} -
probe the next function:
bool IsValidPassword(string password,
int minLength=10,
int maxLength=50,
bool requireLowercase=true,
bool requireUppercase=false,
bool requireNumber=false,
bool requireSimbol=false)
{
if (string.IsNullOrEmpty(password))
{
return false;
}
if (password.Length < minLength || password.Length > maxLength)
{
return false;
}
if (requireUppercase && !Regex.IsMatch(password, "[A-Z]"))
{
return false;
}
if (requireLowercase && !Regex.IsMatch(password, "[a-z]"))
{
return false;
}
if (requireNumber && !Regex.IsMatch(password, "[0-9]"))
{
return false;
}
string pattern= @"[!@#$%^&*()_+\-=\[\]{};':" + "\"" + @"\\|,.<>\/? ~]";
if (requireSimbol && !Regex.IsMatch(password, pattern))
return false;
return true;
} -
What's a simbol?
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
It's a symbol. /ravi
My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com
-
Hi, How can I have a regex for my .NET app's user password with the following: Password MUST be a minimum of 10 AND maximum of 50 It should NOT contain space. It should be MIXED alphabet (upper + lower), numeric and following special characters !@#$%^&*-_+=~ Thanks, Jassim
Apart from the above-mentioned problem with the readability of Regex, another problem is that Regex is quite computation-heavy, and due to the fact that it builds underlying state-machine[^] performance penalty is heavier for big strings.