Validate US Telephone Numbers - Why my function doesn't work? [Solved] [Spoiler]

Hi!

I came up this this solution:

function telephoneCheck(str) {
  var regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?|\d{4}$/;
  return regex.test(str);
}

But I have problem, with this code. When regex is matching only part of a number, my function returns true instead of false.

Any ideas how I can repair this?

Ready for testing: https://repl.it/DtoT/0

1 Like

Hmmm, it’s hard to help with a complicated regex like this without just telling you a regex string that will work!

The tool that unlocked this for me was http://regex101.com which has a test runner built in, and a panel that explains your regex to you as you write it.

The explanation of your current regex looks like this:

Expand for explanation
/^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?|\d{4}$/g
  1st Alternative 
    ^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?
    ^ asserts position at start of the string
      1st Capturing Group (1\s?)?
         ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
         1 matches the character 1 literally (case sensitive)
          \s? matches any whitespace character (equal to [\r\n\t\f\v ])
            ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
      2nd Capturing Group (\(\d{3}\)|\d{3})
        1st Alternative \(\d{3}\)
          \( matches the character ( literally (case sensitive)
            \d{3} matches a digit (equal to [0-9])
            {3} Quantifier — Matches exactly 3 times
          \) matches the character ) literally (case sensitive)
        2nd Alternative \d{3}
          \d{3} matches a digit (equal to [0-9])
          {3} Quantifier — Matches exactly 3 times
    Match a single character present in the list below [\s\-]?
      ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
      \s matches any whitespace character (equal to [\r\n\t\f\v ])
      \- matches the character - literally (case sensitive)
    \d{3} matches a digit (equal to [0-9])
      {3} Quantifier — Matches exactly 3 times
    Match a single character present in the list below [\s\-]?
      ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
      \s matches any whitespace character (equal to [\r\n\t\f\v ])
      \- matches the character - literally (case sensitive)
    2nd Alternative \d{4}$
      \d{4} matches a digit (equal to [0-9])
        {4} Quantifier — Matches exactly 4 times
      $ asserts position at the end of the string
    Global pattern flags
      g modifier: global. All matches (don't return after first match)

It’s pretty verbose, but once you get used to reading it it helps you really understand what your doing.

The test runner is pretty awesome too, since it means you don’t have to keep pasting code into the FCC window and scrolling to the results! The downside is that you have to put all the strings in to the test runner yourself to set it up. That was worth it for me, since I went from ‘never going to complete this challenge’ to ‘Finished in about an hour’ after I started using regex101.

Good luck :slight_smile:

1 Like

Thanks very much!

That I had:

^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?|\d{4}$

This is right:

^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$

This was unnecessary ^(1\s?)?((\d{3})|\d{3})[\s-]?\d{3}[\s-]?|\d{4}$`

Congratulations :slight_smile:

That’s a shorter working regex than I ended up with :slight_smile: