Help with Telephone Number Validator

Hi everybody!

I’ve been working on the Telephone Number Validator and I’ve gotten myself a bit stuck. My strategy was originally to just remove all standard formatting tools in phone numbers (parentheses, spaces, dashes) and then check that everything in the number was digits. If they were all digits and 10 characters long, return true. If they were all digits, 11 characters long, and the first number was a 1, return true. Anything else is false.

However, I realized after that the formatting tools had to be in a certain place so my code didn’t reject some cases it should have. I’m fairly certain my entire problem is incorrect true returns, I didn’t notice any incorrect false returns.

Regex are a pretty big weak point for me. Does anybody know of any strong resources I could use to study them? I’m including my code below as well if anybody would like to give that a look and offer me some advice.

Thanks everybody!

/**
 * telephoneNumberValidator.js
 *
 * Takes in a string and determines whether or not the numbers in it
 * create a valid US Telephone Number.
 *
 * @author Chris Wolf
 * @version December 11, 2018
 * chriswolfdesign@gmail.com
 */

/**
 * Takes in a string and determines whether or not he numbers in it create a
 * valid US Telephone Number.
 *
 * @param {str} -- the original string passed in
 *
 * @return {boolean} true if the string represents a valid US Telephone Number,
 *                   false otherwise
 */
function telephoneCheck(str) {
  let newStr = str;
  newStr = newStr.replace(/\s/g, "");
  newStr = newStr.replace(/\(/g, "");
  newStr = newStr.replace(/\)/g, "");
  newStr = newStr.replace(/-/g, "");

  for(var i = 0; i < newStr.length; i++) {
    if(isNaN(parseInt(newStr[i]))) {
      return false;
    }
  }

  if(newStr.length === 10) {
    return true;
  }

  if(newStr.length === 11 && newStr[0] === 1) {
    return true;
  }

  return false;
}

console.log(telephoneCheck("555-555-5555"));
console.log(telephoneCheck("1 555-555-5555"));
console.log(telephoneCheck("1 (555) 555-5555"));
console.log(telephoneCheck("5555555555"));
console.log(telephoneCheck("555-555-5555"));
console.log(telephoneCheck("(555)555-5555"));
console.log(telephoneCheck("1(555)555-5555"));
console.log(telephoneCheck("555-5555"));
console.log(telephoneCheck("5555555"));
console.log(telephoneCheck("1 555)555-5555"));
console.log(telephoneCheck("1 555 555 5555"));
console.log(telephoneCheck("1 456 789 4444"));
console.log(telephoneCheck("123**&!!asdf#"));
console.log(telephoneCheck("55555555"));
console.log(telephoneCheck("(6054756961)"));
console.log(telephoneCheck("2 (757) 622-7382"));
console.log(telephoneCheck("0 (757) 622-7382"));
console.log(telephoneCheck("-1 (757) 622-7382"));
console.log(telephoneCheck("2 757 622-7382"));
console.log(telephoneCheck("10 (757) 622-7382"));
console.log(telephoneCheck("27576227382"));
console.log(telephoneCheck("(275)76227382"));
console.log(telephoneCheck("2(757)6227382"));
console.log(telephoneCheck("2(757)622-7382"));
console.log(telephoneCheck("555)-555-5555"));
console.log(telephoneCheck("(555-555-5555"));
console.log(telephoneCheck("(555)5(55?)-5555"));

This is a short summary I made a while ago https://drive.google.com/open?id=1ibOGWWIzNTQon__l_CkFs6mj5LgUwCHR. It is dense, so read it carefully and spend some time on the sections. I hope it helps. It is still my summary, so there may be some inaccuracies/poor phrasing etc. But it should help a beginner.

For expert opinion, read the first few chapters of the O’Reilly book Mastering Regex Expressions.
Also, you can test your regex in a tool like https://regex101.com/, it is very useful for debugging. GL!

Thank you Radollvanov. Both of those sources seem very helpful, I’ll give them both a look.

There is also regular-expressions.info

I’m using this regular expression,

/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/

How can I add that it also validates the another format? I’m not good with regular expressions.

Custom software development company