Ageis
Ageis

Reputation: 2315

why does this regex not make special characters compulsary

I have the following regex:

(?=^.{7,14}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+}{":;'?\/>.<,])(?!.*\s).*$

It's meant to match and enforce the following password policy:

However, it allows a password without any special chars.

Upvotes: 0

Views: 44

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89584

The problem comes from the html entities inside the character class (a character class is a set of characters, you can't put strings inside), consequence, strings that contain q,u,o,t,e,g or l succeed.

Instead you can use:

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!-@_{}])\S{7,14}$

(The character class for special characters is shorten using ranges and the ascii table)

Upvotes: 1

Related Questions