Regex with min 8 digits, 20 characters

2k Views Asked by At

I'm currently looking for a regex that verify's the following requirements:

  • Should contain atleast 8 digits (0-9).
  • Between the digits it is allowed to use other characters (a-z) (also upper case).
  • It should contain max 20 characters (a-z 0-9).

example:

12345678: true
123adafa45678: true
123ab456: false (needs atleast 8 digits, now only 6)
ab12345a678: true 
ab123456789afgb2459a2: false (more then 20 characters)

I tried serveral things but if I use something like: (\D*\d\D*){8,} then it works but it doesn't meet the last requirement (up to 20 characters).

1

There are 1 best solutions below

6
On BEST ANSWER

Use a lookahead for 8 digits:

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

See live demo.