Telephone Number Validator regex

Hi! I am using regex to solve this problem. But I am not able to solve the parentheses (spaces/dash) match. Is it possible to solve this with regex?

What is your current full code? It is indeed possible to solve with a regex.

function telephoneCheck(str) {
  let regex = /^([1]{0,1})((-?\s?\(?)(\d{3})(-?\s?\)?))(-?\s?\(?)(\d{3})(-?\s?\)?)(-?\s?\(?)(\d{4})(-?\s?\)?)$/g;

  return  regex.test(str) ;

I would firstly look back at the given examples in the question.
Your regex assumes that brackets can be placed at lots of different points in the phone number. But, when you look at the examples, you’ll see that you only need to check if the brackets are around one particular set of digits.
In other words, you only need to account for two possibilities - either that one set of brackets is there or it isn’t.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.