Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx to Validate Password (1 Lowercase, 1Uppercase, 1 Digit, NoSpaces)

I'm trying to create a RegExpression to meet the criteria below;

  • at least 1 Lowercase
  • at least 1 Uppercase
  • at least 1 Digit
  • No Spaces
  • Minimum 8 characters
  • No special characters

So far I got this;

^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.\s).*$

However I can not get it to work. Any help would be greatly appreciated. I was never good at puzzles :)

like image 929
user1595357 Avatar asked Oct 08 '12 11:10

user1595357


People also ask

What is password should contain atleast 1 special character?

a minimum of 1 special character: ~`! @#$%^&*()-_+={}[]|\;:"<>,./? at least 1 upper case, numeric, and special character must be EMBEDDED somewhere in the middle of the password, and not just be the first or the last character of the password string.


1 Answers

You're nearly there; it's just the .* at the end that ignores your "no spaces/special characters" rules, and the (?=.\s) lookahead is wrong (you probably meant (?!.*\s) or (?=\S*$)).

But you don't need that lookahead anyway because you can simply specify which characters are allowed (and enforce the "8 characters minimum" rule there, too):

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{8,}$

But why do you want to keep users from using non-alphanumeric characters in their passwords?

like image 145
Tim Pietzcker Avatar answered Nov 26 '22 16:11

Tim Pietzcker