This must be simple and I expect it working in the same way but it's not helping me out.
using System;
using System.Text.RegularExpressions;
I am in a need of password validation regular expression
with certain conditions where -
1) It must contain at least a number
2) one upper case letter
3) 8 characters long.
public class Program
{
public static bool IsValidPassword(string plainText) {
Regex regex = new Regex(@"^(.{0,7}|[^0-9]*|[^A-Z])$");
Match match = regex.Match(plainText);
return match.Success;
}
public static void Main()
{
Console.WriteLine(IsValidPassword("shing")); //logs 'True' always
}
}
I've taken regex from this source- Password must be 8 characters including 1 uppercase letter, 1 special character, alphanumeric characters
Issue is that it returns 'True' always and the string that I am sending to method is not valid.
Help me if I am doing something wrong with the regex.
Please play with it here- https://dotnetfiddle.net/lEFYGJ
This regex will enforce these rules: • At least one upper case english letter • At least one lower case english letter • At least one digit • At least one special character • Minimum 8 in length refered from : Regex for Password Must be contain at least 8 characters, least 1 number and both lower and uppercase letters and special characters
Password Validation Program in C++. Steps involved are:-The user provides the password which is to be check. The password provided by the user will now be check for uppercase, lowercase, and digits. The strongness of the password is classified into 3 categories which are, strong, moderate, and weak.
It is clear as rain. See Lookahead Example: Simple Password Validation if you ever have any doubt when using regex for password validation. BTW, your current code does not account for Unicode upper case letters. I have created a simple method to validate all kind of password.
Firstly the length of the password has to be checked then whether it contains uppercase, lowercase, digits and special characters. If all of them are present then the method isValid (String password) returns true.
I recommend you create separate patterns to validate the password:
var input = "P@ssw0rd";
var hasNumber = new Regex(@"[0-9]+");
var hasUpperChar = new Regex(@"[A-Z]+");
var hasMinimum8Chars = new Regex(@".{8,}");
var isValidated = hasNumber.IsMatch(input) && hasUpperChar.IsMatch(input) && hasMinimum8Chars.IsMatch(input);
Console.WriteLine(isValidated);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With