iowatiger08
iowatiger08

Reputation: 1962

regex for complex passwords

Requirement is start with one alpha has a number has both lower and upper case alpha has special character. my regex almost gets this:

^[a-zA-Z](?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[@#$_!%\*])([a-zA-Z0-9@#$_!%\*]+)

Tried against and does not pass as expected

e2~Sertty
2e!Sertty
E2sertty
pers1234!

Apers1234! should work but does not
R4pers1234! same
Rtpers1234!

pApers1234! does work
TAers1234! works

Ideas?

**I removed the length requirement for now but wanted to have min length of 8

Upvotes: 0

Views: 71

Answers (2)

anubhava
anubhava

Reputation: 785541

Problem is that you're matching [a-zA-Z] first before the lookaheads.

This should work:

/^(?=.*[a-z])(?=.*[A-Z])(?=\D*\d)(?=.*[@#$_%\*!])([a-zA-Z][\w@#$!%*]{7,})$/

RegEx Demo

Upvotes: 2

adrCoder
adrCoder

Reputation: 3275

how about this?

^[a-zA-Z](?=.*[a-zA-Z])(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[@#$_!%\*])([a-zA-Z0-9@#$_!%\*]+)

works for these :

Apers1234! should work but does not
R4pers1234! same
Rtpers1234!

Upvotes: 1

Related Questions