RegExs VALIDATION

Viewed 42

I have two different things password and pin. I'm looking for RegEx formate for both like below -:

Password -:

  1. 1 Capital latter
  2. 1 Small latter
  3. 1 Special character
  4. Min 8 digits
  5. 3 time can't repeat any thing.

I have achieved 4 points by

let reg = "^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})"

but i'm not able to achieve 5th one.

PIN

 1. 6 digit pin
 2. Only numbers
 3. Can't be use simple pin (111111,222222,...00000 and 123456, 234567 ....654321)
1 Answers

You may use this regex with an additional negative lookahead to disallow 3 or more repeats of any character:

^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?!.*?(.)(?:.*?\1){2}).{8,}$

RegEx Demo

Take note of this additional negative lookahead:

(?!.*?(.)(?:.*?\1){2})

This will fail the match if we find 3 or more repeats of a character anywhere in input.

Complete regex details:

  • ^: Start
  • (?=.*[a-z]): Positive lookahead to assert that we must have a small letter
  • (?=.*[A-Z]): Positive lookahead to assert that we must have an uppercase letter
  • (?=.*[0-9]): Positive lookahead to assert that we must have a digit
  • (?=.*[!@#$%^&*]): Positive lookahead to assert that we must have a special character
  • (?!.*?(.)(?:.*?\1){2}): Negative lookahead to assert that we must not 3 or more repeats of same character
  • .{8,}: Match 8 or more characters
  • $; End
Related