0

I try to make a valid html5 pattern for a Password. It should be at least 9 characters long and contain at least one Uppercase, one lowercase, one digit and one specialcharacter of this list

()[]{}?!$%&/=*+~,.;:<>-_

I made this regex but it doesn't work... anyone can fix this?

pattern="^(?=.*\d)(?=.*[a-z])(?=.*[()[]{}?!$%&/=*+~,.;:<>-_])(?=.*[A-Z]).{9,}?$"

1 Answer 1

1
pattern="(?=.*\d)(?=.*[a-z])(?=.*[()\[\]{}?!$%&/=*+~,.;:<>_-])(?=.*[A-Z]).{9,}"

There are several errors:

^(?=.*\d)(?=.*[a-z])(?=.*[()[]{}?!$%&/=*+~,.;:<>-_])(?=.*[A-Z]).{9,}?$
# ] needs to be escaped ----^^                  ^                   ^
# otherwise it will close the character class   |                   |
# [ too but for no logical reason               |                   |
# the - is used to define a character range ----+                   |
# the range >-_ gives >?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_            |
# there's no reason to make this quantifier non-greedy -------------+

In addition, anchors ^ and $ are implicit, you don't have to put them.

Note that using ranges, you can also write the pattern like that:

pattern="(?=.*\d)(?=.*[a-z])(?=.*[!$%&(-/:-?_{}~])(?=.*[A-Z]).{9,}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.