pita
pita

Reputation: 537

Regular expression for passwords with special characters

Here is the regular expression i fount from microsoft's website

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$ 

and it Validates a strong password. It must be between 8 and 10 characters, contain at least one digit and one alphabetic character, and must not contain special characters.

But now we decide to allow user using special characters in their passwords, so how do I modify this regular expression? I don't quite understand why put ?! in front.

Upvotes: 1

Views: 6093

Answers (1)

stema
stema

Reputation: 93026

(?!^[0-9]*$) is a negative lookahead. This assertion fails if there are only digits from the start to the end. So, you have different possibilities:

I would rewrite those conditions to require at least one and not to forbid only that characters.

(?=.*\d) would require at least one digit

(?=.*[a-zA-Z]) would require at least one letter

Your regex would then look something like this:

^(?=.*[0-9])(?=.*[a-zA-Z]).{8,10}$

means require at least one digit, one letter and consist of 8 to 10 characters. The . can be any character, but no newlines.

See it here at Regexr

Upvotes: 3

Related Questions