Telephone Number Validator Regex Question

Telephone Number Validator Regex Question
0.0 0

#1

My solution is 95% done . Can anyone tell me why my regex isn’t working for all cases?

It should read match starting with number one optionally, followed by zero to many non digits, followed by a bracket optionally if looking ahead there is 3 digits and another bracket, followed by 3 digits, followed by zero to many non digits, followed by 3 digits, followed by zero to many non digits, followed by 4 digits at the end.

  const regex = /^1?\D*\(?(?=\d{3}\))?\d{3}\D*\d{3}\D*\d{4}$/;

#2

Your current regex is checking for 0 or 1 parenthesis before and after the 3 digit area code. This will match strings that have either one of the required parenthesis. For example "(555-555-5555" will match

You need to check that the string has both parenthesis around the 3 digit area code, or no parenthesis at all.

I ended up breaking this into several steps to simplify the regex a bit:

  1. Normalize area code - remove valid parenthesis from around the area code if present
  2. Then do regex