Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String regex not working

Tags:

c#

regex

I have the following regex in c sharp to check if supplied password is

  • more than 10 characters
  • should have at least one lowercase character
  • should have at least one Uppercase character
  • Should have either a number or a special character

Regex.IsMatch(password, "^.*(?=.{10,})(?=.*[0-9]|[@#$%^&+=])(?=.*[a-z])(?=.*[A-Z]).*$")

Why wouldn't the above work?

its taking abcdefgh123 but not abcdefgh&+

like image 557
chuckyCheese Avatar asked Dec 08 '22 00:12

chuckyCheese


1 Answers

Personally I'd do a separate check for the length and then one check for each of the character requirements. I don't like to use overly complicated regular expressions when things can be done in a more readable manner.

if (password.Length > 10 && 
    Regex.IsMatch(password, "[a-b]") && 
    Regex.IsMatch(password, "[A-Z]") && 
    Regex.IsMatch(password, "[0-9@#$%^&+=]"))
{
    //Valid password
}
like image 119
juharr Avatar answered Jan 02 '23 07:01

juharr