Can someone give me the regex to match a valid AWS Cognito password - with numbers, special characters (their list), lower and upper case letters
The AWS Cognito default length limit is 6 characters and has it's own list of special characters
Can someone give me the regex to match a valid AWS Cognito password - with numbers, special characters (their list), lower and upper case letters
The AWS Cognito default length limit is 6 characters and has it's own list of special characters
/^(?!\s+)(?!.*\s+$)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[$^*.[\]{}()?"!@#%&/\\,><':;|_~`=+\- ])[A-Za-z0-9$^*.[\]{}()?"!@#%&/\\,><':;|_~`=+\- ]{8,256}$/
/
Indicates the start of a regular expression.^
Beginning. Matches the beginning of the string.(?!\s+)
Disallows leading spaces.(?!.*\s+$)
Disallows trailing spaces.(?=.*[a-z])
Requires lowercase letters.(?=.*[A-Z])
Requires uppercase letters.(?=.*[0-9])
Requires numbers.(?=.*[\^$*.[\]{}()?"!@#%&/\\,><':;|_~`=+\- ])
Requires at least one special character from the specified set. (The non-leading, non-trailing space character is also treated as a special character.)[A-Za-z0-9^$*.[\]{}()?"!@#%&/\\,><':;|_~`=+\- ]{8,256}
Minimum 8 characters from the allowed set, maximum 256 characters.$
End. Matches the end of the string./
Indicates the end of a regular expression.The minimum character limit defaults to 8 but can be customised to a value between 6 and 99. The full length of a password however is limited to 256 characters (not 99).
https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-policies.html
Cognito also has a "no leading or trailing spaces" rule in the default password requirements, but there are several docs out there that incorrectly state that "The space character is also treated as a special character". However, the current behaviour is actually "The non-leading, non-trailing space character is also treated as a special character".
To see the correct default password rules, view a user pool, click on the "Sign-in experience" tab, and click on "Contains at least 1 special character" to bring up a tooltip with the rules.
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[\^$*.\[\]{}\(\)?\-"!@#%&\/,><\':;|_~`])\S{8,99}$/
–
Langill (^ $ * . [ ] { } ( ) ? - " ! @ # % & / \ , > < ' : ; | _ ~ `` + =)
–
Alan \S
is a rather large mistake in your regexp. Now this may have been a little different when you put the regexp together, however according to AWS today a password is allowed to contain upper & lower case basic latin letters, numbers, and special characters from the list. It must also contain fewer than 99 characters so you need to reduce the max length by 1. \S
will allow anything that isn't whitespace, so as long as the password satisfies the "contains a *" lookaheads it can contain any other characters that aren't in the allowed set. Continued... –
Hornet /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[\^$*.\[\]{}\(\)?\"!@#%&\/\\,><\':;|_~`=+\-])[a-zA-Z0-9\^$*.\[\]{}\(\)?\"!@#%&\/\\,><\':;|_~`=+\-]{8,98}$/
The current Cognito password requirements specifications are available here: docs.aws.amazon.com/cognito/latest/developerguide/…. (I'd add this answer myself but some bright spark closed this question because it looked similar to another one) –
Hornet /[\^$*.\[\]{}\(\)?"!@#%&\\\/,><\':;|_~`=+\- ]/;
–
Arriola (.)
at the end of the regex: /^(?!\s+)(?!.*\s+$)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[$^*.[\]{}()?"!@#%&/\\,><':;|_~`=+\- ])[A-Za-z0-9$^*.[\]{}()?"!@#%&/\\,><':;|_~`=+\- ](.){8,256}$/
–
Biceps the Regex formula for Swift 5 is
"(.*[()!@^$*.?\\-@#%&\":;|><'_~`+=\\[\\],{}])"
These are the default requirements based on the AWS console settings and the current Cognito documentation:
1 The restriction to Arabic numerals is not specified in the documentation, but I experimented and found that other Unicode digits do not count toward this requirement
2 The accepted answer limits the allowed characters to the characters that are required, however more characters are allowed than are required. The Cognito documentation says, "After Amazon Cognito verifies that passwords contain the minimum required characters, your users' passwords can contain additional characters of any type up to the maximum password length." Cognito is perfectly happy with the following password, for instance: Ab!~1 🤢℉”•—”†„简体字
.
This regex handles all of the above scenarios:
/^(?!\s+)(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\^$*.[\]{}()?"!@#%&/\\,><':;|_~`=+-]).{8,256}(?<!\s)$/
Explanation:
^(?!\s+)
No leading spaces(?=.*[A-Z])
At least one uppercase letter(?=.*[a-z])
At least one lowercase letter(?=.*[0-9])
At least one digit(?=.*[\^$*.[\]{}()?"!@#%&/\\,><':;|_~`=+-])
At least one of: ^ $ * . [ ] { } ( ) ? " ! @ # % & / \ , > < ' : ; | _ ~ ` = + - (listed in the same order as in the Cognito documentation).
Other than leading and trailing spaces, any characters are allowed,
including spaces and emojis{8,256}
8-256 characters(?<!\s)$
No trailing spaces© 2022 - 2025 — McMap. All rights reserved.
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[=+\-^$*.\[\]{}()?"!@#%&/\\,><':;|_~`])\S{8,99}$/
– Functionary