Duc
Duc

Reputation: 501

Password Regular Expression

I need to generate a password regex with the following criteria:

A valid password must contain at least 8 characters. It must have at least one uppercase, one lowercase and one non alphabetic character.

So far, I created this pattern:

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

But it still taking String that has no non alphabetic character. How can I do this? Thanks

Upvotes: 1

Views: 297

Answers (1)

Anirudha
Anirudha

Reputation: 32797

You can try this

                                           |->match 8 or more chars
                                         -----
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[^a-zA-Z]).{8,}$
 ------------ ---------- ---------------
      |           |              |->matches further only if there is atleast 1 non alphabetic char
      |           |->matches further only if there is atleast 1 a-z
      |->matches further only if there is atleast 1 A-Z

Upvotes: 4

Related Questions