I am using regex for validate password
this is my regex configuration
@"^(?!.*[\s])(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,32}$"
My criteria is
- The password must contain at least one uppercase letter, one lowercase letter and a number;
- It cannot have any special character, accent or space;
- In addition, the password can be 6 to 32 characters long.
Tests I was waiting for:
- BIT123456 Is Invalid
- Year2021 Is Valid
Fing!2020 Is Invalid ( but it returns valid )
My code to check for errors:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
var totalTestCases = int.Parse(Console.ReadLine());
string S = "";
string pattern = @"^(?!.*[\s])(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,32}$";
var n = 0;
do
{
n++;
S = Console.ReadLine();
if (S != "")
{
// Console.WriteLine("{0}", Regex.IsMatch(S, pattern) ? "Senha valida." : "Senha invalida.");
string test = Regex.IsMatch(S, pattern) ? "Password valid." : "Password invalid.";
Console.WriteLine(test);
}
} while (n < totalTestCases);
}
}