Help validate US telephone numbers algorithm

function telephoneCheck(str) {
  
   var reg=str.replace(/\**&!!asdf#/," ").replace(/\?/,' ').replace(/[\)]/," ").replace(/[\(]/," ");
  if(str==reg){
    return true;
  }
  else{
    return false;
  }
}


telephoneCheck("27576227382");

My algorithm is working for some inputs. please help me how can i use more regular expressions to validate all the inputs.

Your test is flawed. Even if the original string is made of valid characters, it can still have more (or fewer) digits than a phone number and it can have spaces, dashes or parenthesis at the wrong places. Take these for example:
555(555(5555
5-5-5-5—5-5-5
5

If you want to check the number with regex, you should test if the string corresponds to one of the accepted patterns. That way you’ll know that you have the correct characters, that you have the right amount of everything, and that all the characters are where they should be.

The simplest pattern is number * 10. Or you can express it as number * 3 then number * 3 then number * 4

Just expand your code try to solve the other tests one by one.

I suggest you readup on regexp and play with it here https://regex101.com. When you learn a bit, this challenge becomes easy.

I just sat down to do this algorithm, and followed this advice. I fed each test into the test runner at http://regex101.com and changed the regex to pass each new test. Took some time, and the resulting re is horrible to look at, but it passes!

Thanks @Nepherius!

1 Like

This is my solution for this problem.

Ill also paste it here since code isnt very big+

function telephoneCheck(str) {
  var pattern=/((^1((\s\d)|(\s\(\d)|(\d)|(\(\d)))|(^\(\d|^\d))\d{2}((\)\s)|(\)-)|-|\s|\))\d{3}[\s-]\d{4}$|^\d{10}$/;
  if (pattern.exec(str)&&str.indexOf('(')>=0&&str.indexOf(")")>=0)
  {return true;}
  else if (pattern.exec(str)&&str.indexOf('(')<0&&str.indexOf(")")<0)
  {return true;}
  else {return false;}
}
telephoneCheck("1 555-555-5555");

As you can I see I tried to stay within the realm of RegExp when solving this challenge. However I failed to understand how to perform a check for ‘broken parenthesis’ - where only one of a pair () is presented in string with RegExp instrumentary. So I deserted to ugly JS hack which half-assedly checks for presence of second ( (without much care of what is the distance between them).

This code passed challenge but Id still like to know how can i solve this with pure RegExp? My clue was to use lookbehind somehow but I didnt find a proper example for such problem.

Here was my monstrosity. Not sure if it’s the cleanest way to do it, but it was a pure regex solution, using the method described above by @Nepherius.

It’s worth pasting into regex101.com as it will explain each section to you in a nice amount of detail :slight_smile:

Spoiler
 var re = /^[1]?[ ]?(\d{3}|[(]\d{3}[)])([ ]?|[-])(\d{3}|[(]\d{3}[)])([ ]?|[-])(\d{4}|[(]\d{4}[)])$/;

Hi, why did you envelop 1 in square brackets []? Arent squares for alternative picking from multiple characters? i.e [12] will match for either 1 or 2.

I honestly don’t remember - it was a month ago :slight_smile:

Like I said above - I don’t claim for it to be the best approach, but after a lot of trial and error it worked, and I was happy to move on!

That;s some impressive regex wizardry, my solution was a bit different:

function telephoneCheck(str) {
  if (isNaN(str[0]) && !isNaN(str[4]) || str.match(/\d[^-|\d|\s]/g) !== null && str.match(/\(\d+\)/g) === null) {
    return false;
  } else if (str.match(/\(/g) !== null && str.match(/\)/g) === null) {
    return false;
  }
  str = str.replace(/\-|\(|\)|\s/g, '');
  if (!isNaN(str)) {
    if (str.length < 10 || str.length === 11 && str[0] != 1) {
      return false;
    } else if (str.length > 11) {
      return false;
    }
    return true;
  }
  return false;
}

telephoneCheck("555-555-5555");

JacksonBates you ripper, thanks man, by busting my chops on this for some hours. While i can rap my head around most of what has been throw to me, regex is one thing i’ve always found difficult to understand. But i’m one person who learns well by watching/seeing an example. Your code made me understand regex a bit more. Thanks man. Simple and effective.

Cool! You should check this out - I only saw it recently but it was another thing that really helped me understand regex: