Question

I am using regex pattern match for passwords.
There are only three constrains on my password.

1. There must be at least 1 UPPER-CHARACTER.
2. There must be at least 1 special char from given list.
3. There must be at least 8 characters length.

I used this regex : [!@#\$%\^\&*?+=._-]{1,}[a-z0-9]{6,}[A-Z]{1,}$.
but it matters sequence. Sequence must not matter at all. Any Idea?

Was it helpful?

Solution

The following regex should work:

^(?=.*[!@#\$%\^\&*?+=._-])(?=.*[A-Z]).{8,}$

Regular expression visualization

OTHER TIPS

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

This should work.

Here is what I would go with:

(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*[!#\$%&\?])^\D.{7}

Note that the .* after each look-ahead term was superfluous.

(?!...) is a negative look-ahead, to make sure there are no special characters.

^\D requires that the first character be a non-digit. Then I simply require 7 characters after that, because the end is not enforced.

But why exclude special characters from passwords? Usually just the opposite is encouraged.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top