JavaScript Algorithms and Data Structures Projects: Telephone Number Validator some help!

Hello!
I have been working on the Telephone Validator project for the past hour and the program has passed all but two of the tests. The two tests are:

telephoneCheck("5555555555") should return true.
telephoneCheck("(555-555-5555") should return false.

I’ve been tinkering around but haven’t been able to get the regex quite right. Also, been looking up Coding Train’s video series on regex and that helped IMMENSELY. I’ll appreciate any feedback given! Thanks so much.

function telephoneCheck(str) {
  // regular expression stored into a variable
  let regex = /^(1\s?)?(\(\d{3}[-.\s?)?]|\d{3})[\s?\-]?\d{3}[-.\s?]\d{4}/;
  return regex.test(str);
}

I believe my difficulty comes from not checking the area code pattern (the first 3 digits that aren’t the country code 1) of the regex correctly. I’ve been trying to check for having the open parentheses in the first three digits are followed by a closing parenthesis and checking for a case where the user submitted a number with no spaces.

Best,
Edgar.

https://www.regexpal.com/

These are great tools to figure out your regex with. This way you can tweek and see.

and here is a cheat sheet if you do not have one

1 Like

I don’t have a cheatsheet actually, this is gonna help a TON!!

wow that cheatsheet is in depth