AWS Cognito Password Regex

Jonathan Irwin
2 min readJun 29, 2020

Let your users know if they meet your password requirements

Photo by Chris Barbalis on Unsplash

If you are using AWS Cognito there will come a time when you require a regular expression to check that users meet your password strength requirements

AWS Cognito Password Strength Options

Solution

/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[\^$*.\[\]{}\(\)?\-“!@#%&/,><\’:;|_~`])\S{8,99}$/

That regex covers the default case where all boxes are selected. If you want to adjust the selection see the explanation below on how to make it work for your use-case.

Explanation

  • / Indicates the start of a regular expression
  • ^ Beginning. Matches the beginning of the string.
  • (?=.*[a-z]) Requires lowercase letters
  • (?=.*[A-Z]) Requires uppercase letters
  • (?=.*[0-9]) Requires numbers
  • (?=.*[\^$*.\[\]{}\(\)?\-“!@#%&/,><\’:;|_~`]) Requires special characters (only the special characters listed by AWS Cognito).
  • \S Whitespace (space, tab, carriage return) not allowed
  • {8,99} Minimum 8 characters, maximum 99 characters
  • $ End. Matches the end of the string.
  • / Close.

Links

You can find an interactive example here

You can find a Stack Overflow question on the subject here

--

--

Jonathan Irwin

I scream.. You scream.. The police come.. It’s awkward..