US Phone Number Validation - Help Needed

I got a regex from Stack Overflow for phone number validation, but it doesn’t get all of the tests to pass.

The test results are:

telephoneCheck("1 555-555-5555") should return true.
telephoneCheck("1 (555) 555-5555") should return true.
telephoneCheck("1(555)555-5555") should return true.
telephoneCheck("1 555 555 5555") should return true.
telephoneCheck("1 456 789 4444") should return true.

The rest of the tests pass.

I tried to make the space after the parentheses with the three digit state code and the hyphens optional by adding asterisk after each one. Is there a better way to do that?

My code so far


function telephoneCheck(str) {
return str.match(/^(\([0-9]{3}\) *|[0-9]{3}-*)[0-9]{3}-*[0-9]{4}$/) !== null;
}

telephoneCheck("1 456 789 4444");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 Edg/84.0.522.44.

Challenge: Telephone Number Validator

Link to the challenge:

I changed my code to this after reading the hints here (I haven’t looked at the solutions yet and I’m trying to not look):

function telephoneCheck(str) {
  let hasTenDigits = false;
  let hasElevenDigits = false;
  let startsWithOne = false;
  let hasPermittedCharsOnly = false;
  let hasCorrectParentheses = false;

  const tenDigitRegex = /\d{10}/;
  const elevenDigitRegex = /\d{11}/;
  const startWithOneRegex = /^(1){1}/;
  const permittedCharsRegex = /[0-9\-\(\) ]/;
  const correctParenthesesRegex = /^\([0-9]{3}\)$/;

  if (str.match(tenDigitRegex)) {
    hasTenDigits = true;
  }

  if (str.match(elevenDigitRegex)) {
    hasElevenDigits = true;
  }

  if (str.match(startWithOneRegex)) {
    startsWithOne = true;
  }


}

How do I check whether the last two Boolean values are true or not? Are my regular expressions for those alright? What about my regular expressions for the other three tests?

maybe use test instead of match?

if you need all of them to be true you can return false as soon as you find a false one, and true eif everything is true

I know I need to return false when even one is false, so that’s not what I’m asking. I’m trying to ask about the regular expressions specifically. What expressions do I need for the last two tests?

let hasPermittedCharsOnly = false;
let hasCorrectParentheses = false;

For these two.

And are the other regular expressions I have alright?

I’m doing it like this instead:

function telephoneCheck(str) {
  const tenDigitRegex = /\d{10}/;
  const elevenDigitRegex = /\d{11}/;
  const startWithOneRegex = /^(1){1}/;
  const permittedCharsRegex = /[0-9\-\(\) ]/;
  const correctParenthesesRegex = /(\([0-9]{3}\))*/;

  let hasTenDigits = tenDigitRegex.test(str);
  let hasElevenDigits = elevenDigitRegex.test(str);
  let startsWithOne = startWithOneRegex.test(str);
  let hasPermittedCharsOnly = false;
  let hasCorrectParentheses = false;
  

}

Anyway, I still need good regular expressions for the last two tests:

let hasPermittedCharsOnly = false;
let hasCorrectParentheses = false;

Edit: I changed the regex for hasCorrectParentheses. Is it okay or do I need to change it?