Jofairden
Jofairden

Reputation: 95

regex for passwords 2numbers, 1capital, 4lowercase

Im looking for a regex that checks for:

a minimum of 2 numbers and

a minimum of 1 capital letter and

a minimum of 4 lowercase letters

And also checks for:

a maximum of 30 characters

I've been trying to make this but all my creations don't work :) You can leave the maximum out too if you can't do it, I could check it in another way.

Upvotes: 0

Views: 40

Answers (2)

Jiri Kremser
Jiri Kremser

Reputation: 12837

I guess the order of those conditions is arbitrary. Therefore there is no need to do it using just 1 regexp. For each condition, you can have 1 regexp and then you can do a logical conjunction in your favorite language and it would be much more readable than one super-cool-ninja-regexp.

  • a minimum of 2 numbers

".*\d.*\d.*"

  • a minimum of 1 capital letter and

".*[A-Z].*"

  • a minimum of 4 lowercase letters

".*[a-z].*[a-z].*[a-z].*[a-z]"

  • a maximum of 30 characters

".{6,30}"

Upvotes: 2

Sabuj Hassan
Sabuj Hassan

Reputation: 39385

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

But if you want only the alphanumerics, then:

^(?=.*\d.*\d)(?=.*[A-Z])(?=.*[a-z].*[a-z].*[a-z].*[a-z])[a-zA-Z0-9]{7,30}$

(?=.*\d.*\d): at least two digits

(?=.*[A-Z]): one caps letter

(?=.*[a-z].*[a-z].*[a-z].*[a-z]): minimum four lowercase

[a-zA-Z0-9]{7,30}: length between 7-30

Upvotes: 1

Related Questions