A regex that doesn't accept 2 dot and 2 comma consecutively - Allows letters only

Viewed 54
  • It only allows letters including ñ and Ñ
  • doesn't allow other special character besides comma and dot
  • doesn't allow 2 consecutively dot and comma in any part of the name

What should I add to my regex?

^[a-zA-Z]+$
1 Answers

I would phrase your regex as:

^(?!.*[.,]{2})[A-Za-zñÑ.,]+$

Explanation:

  • ^ from the start of the string
  • (?!.*[.,]{2}) assert that no consecutive dots or commas occur
  • [A-Za-zñÑ.,]+ match a letter, including ñ and Ñ, dot or comma, one or more times
  • $ end of the string
Related