Telephone Number Validator problem

I can’t pass two of the requirements because of the length. where do i place this {3}:

Your code so far


function telephoneCheck(str) {
  // Good luck!
  let regex = /^(1\s?)?(\(\d+\)|\d+)(\s|\-)?[\d+]{3}(\s|\-)?[\d+]{4}$/
  if(str.match(regex)) {
    return true;
  }
  return false;
}

console.log(telephoneCheck("555-555-5555"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36.

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

There is a really useful site for testing strings against custom-made regexes: regex101.com.

You should test the false positives against your regex on that site to figure out why they’re passing (ex: test “55555555” --> your function returns true but it should return false).

1 Like

Hi,

Here is the part where your code is wrong. The \d+ is matching digits 1 or more times (jut right to Infinity). Instead of matching one or more times it should match only three times, therefore you have to use quantifiers. You should probably now what are they and how they work, since you are using them later in your regexp:

  • [\d+]{3}
  • [\d+]{4}

This cheat sheet will be very helpful to you.

And of course @Dusch4593 is right, once you made a change to your regexp, you should try how it works and regex101.com is a great resource.

1 Like

I figured it out already. Thanks so much y’all.

1 Like

Hey mate, I guess I am as stuck you were. My regex wrongly validates two strings when it shouldn’t. I can’t finish my challenge because of these two:

telephoneCheck("27576227382") should return false.
telephoneCheck("(275)76227382") should return false.

And this is my regex:

function telephoneCheck(str) {
  return str.match(/^[1]?\s?(?:\(\d{3}\)|\d{3})\s?-?\d{3}\s?-?\d{4}/g) ? true : false;  
}

Can you give me some hints? :wink:

I just found out what I was missing. The character at the end of m regex $ to make sure my matching string ended that way. Cheers!!

2 Likes

Great!!. My apologies for the late response

No worries mate.

Cheers!