Validate US Telephone Numbers Help

Hi guys, I think i’m close on this but one bit i’m getting really stuck on. Apparently if there’s brackets its ok but if there’s broken brackets (open bracket but not close or vice versa) then it should return false. But I can’t figure out how do i test for this?? Any help would be really appreciated. Thanks

function telephoneCheck(str) {

  //get rid of dashes
  var num = str.replace(/-/g,"");
  //get rid of spaces
  var num2 = num.replace(/\s/g,""); 
  //print to check it
  console.log(num2, num2[0], num2.length);
  //check if has broken brackets
    //how do i do it????? :(
  //check if it has a country code and if so if it is 1
  if (num2.length == 11 && num2[0] !== "1") { 
      return false;
  //if it is 1 then proceed to further checks
  } else { 
    //turn string into a number
    var pureNum = parseInt(num2); 
    //print to check it
    console.log(str, pureNum);
    //check if only numbers and at least 10 digits 
    if (/[0-9]{10,}/.test(pureNum)) { 
      return true; // if true then input is validated as a correct phone number
    } else {
      return false; // if false then input is not validated as a correct phone number


i’d iterate through each character and keep count of brackets. If at any point the number of closed brackets is greater than number of opened brackets, or at the end the numbers of open and closed brackets are different, that’s the indication that the telephone number is invalid.

You can doit that way, but I ended up scrapping mine and reading up on regExp. More than whats given in the challenge description.

Use this site to test it!

Do you have any good articles/websites that explain Regex and can help me with this? I’ve been reading around but am still stumped so far

There are some, but I would recommend you to just play with regex101 + MDN. Regex101 has a regex library - check that out. There are some telephone related regexes that should point you at the right direction while not solving the problem for you.

This site helped me.

Everyone here suggested regex, rightly so, and even provided an awesome resource to learn it… thats great.
I just wanted to share that I initially tried to solve this problem using regex then things got more complicated than i preferred, so i decided to keep the regex at the minimum. So… this is what I did … i simply iterated over the phone number (provided as a string) and converted all digits ( using regex /[0-9]/) to “5” and left the ‘-’ and ‘(’ as is. Now i had a newString in the 555555… format with the dashes and the brackets. which i then ran through a IF else if condition to match one of those 4/5 formats provided. If it passed the IF condition, its a valid format else an invalid one.

Oh yeah… to account for the area code… i split the phone string ( on spaces) and if the first element was of length one and was equal to 1 then went through the rest of the IF condition. If the length is one and its not equal to 1 then return a false/invalid.

At the very least , i hope this gives you a different prospective ( not sure efficient or not… ) to solve this problem.
Good luck.

its pretty doable with a regex, the strategy i used was like:
ok it can start with a ‘1’, a '1 ’ (1-space), but it doesn’t have to.

so that would be (1|1 )? something like that. || is or. it can be one, || one-space, and ? is optional.

then it can be 3 numbers, or 3 numbers with parens

so that would be [0-9]{3} || ([0-9]{3}) the backslashes are t escape the parens. this way if they start parens, they have to close em. and in this way you can do the whole thing

There’s probably more just like this but I found this one first and I really like it. You can test your regex to see how it works or doesn’t work. REally useful. And if you get something you like, you can save it. (beware, the save is kind of buggy)