-3

I want to create a regular expression for password which must have

  1. a minimum of 7 characters
  2. with at least 1 number and 1 alphabet within that first 7 characters.

1 Answers1

0

Since the characters after the first 7 do not require checking, you can run a regex just on the first 7:

/^(?=.*[0-9a-zA-Z]).{7}$/.test(password.substring(0,8))
  • ^ starts with
  • (?=.*[0-9a-zA-Z]) positive lookahead: must contain at least one of these characters
  • .{7} match exactly 7 characters
dcollien
  • 1,234
  • 1
  • 8
  • 5