Telephone Number Validator

Telephone Number Validator
0.0 0

#1

Tell us what’s happening:

The problem is that it matches anything even with any number other than 1, a fix for that please? I tried for 3 hours for a solution but regex is not my strong side.

Your code so far


function telephoneCheck(str) {
return /(1\s)?(\([0-9]{3}\)|[0-9]{3})(\s|-)?[0-9]{3}(-|\s)?[0-9]{4}/g.test(str);
}

telephoneCheck("555-555-5555");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/telephone-number-validator


#2

Nevermind. I solved it.


#3

This is my solution.

function telephoneCheck(str) {
  // Good luck!
  return /^(1\s|1)?(\(\d{3}\)|\d{3})(-|\s)?\d{3}(-|\s)?\d{4}$/.test(str);
}

telephoneCheck("555-555-5555");