Regex pattern in JavaScript to test for dots/hyphens in names

36 Views Asked by At

Required regex pettern like

  1. allow only letters with min 1 dot and maximum 3 dots.
  2. allow optional as '-' special character.

Ex: raj.gopal-reddy or raj.reddy.gopal

1

There are 1 best solutions below

0
Tim Biegeleisen On

Your current regex is on the right track, and I would use a negative lookahead to assert that more than three dots do not occur. Consider this pattern:

^(?!.*\..*\..*\..*\.)[A-Za-z-]*\.[A-Za-z-]*$