1

I want to create regex for

  • Minimum 1 character and 1 number in password
  • Prevent the “4 consecutive characters “ must be in place as well. Eg: 1234, abcd
  • The minimum 8 characters and the maximum is 20

I have tried:

^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$

^(?!.*?(?:0(?:12|98)|123|234|3(?:45|21)|4(?:56|32)|5(?:67|43)|6(?:78|54)|7(?:89|65)|876|987))(?!.*?(.)\\1{2})[0-9]{8}$

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

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

But not able to get result in one regex. Please help me if any one have idea for that. Thanks in advance!

adiga
  • 34,372
  • 9
  • 61
  • 83
Sachin Dobariya
  • 744
  • 7
  • 16

1 Answers1

0

I don't think a regex is the right tool for the job here.

A regex engine doesn't have any concept of the natural ordering of alphanumeric characters so it's not really possible to do it in a concise way.

You could check and exclude results containing:

abcd | bcde | cdef | defg | efgh | fghi | ghij | hijk | ijkl | jklm | klmn | lmno | mnop | nopq | opqr | pqrs | qrst | rstu | stuv | tuvw | uvwx | vwxy | wxyz | 0123 | 1234 | 2345 | 3456 | 4567 | 5678 | 6789

But you definitely should not.