Phone Validator @ FCC

I’m working on the Logic Certificate at Free Code Camp and this is for the Phone Validation Challenge. I know this exercise is mostly about regular expressions & I feel like I’ve found a decent one from Stack Over Flow. I think where I’m confusing myself is around line 7 when comparing var result for matches, Not a Number as a conditional for forking the output

function telephoneCheck(str) {
  // ^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
  let numString = str;
let numRegex = /^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/g; // Change this line
let result = numString.match(numRegex);
  //close, but no cigar.. 
  if (isNaN(result)){    
    console.log("true");
    return true;
  }
  else{    
    console.log(str);
    //  console.log(result);
    console.log("false");
    return false;
  }
}
telephoneCheck("1 555-555-5555");// should return true.
telephoneCheck("1 (555) 555-5555");// should return true.
telephoneCheck("5555555555");// should return true.
telephoneCheck("(555)555-5555");// should return true.
telephoneCheck("1(555)555-5555");// should return true.
telephoneCheck("1 555 555 5555");// should return true.
telephoneCheck("1 456 789 4444");// should return true.
telephoneCheck("555)-555-5555");// should return false.
telephoneCheck("(555-555-5555");// should return false.
//telephoneCheck("555-555-5555");// should return true.
//  telephoneCheck("1 (555) 555-5555");// should return true.
//  telephoneCheck("5555555555");// should return true.
//  telephoneCheck("555-555-5555");// should return true.
//  telephoneCheck("(555)555-5555");// should return true.
//  telephoneCheck("1(555)555-5555");// should return true.
//  telephoneCheck("555-5555");// should return false.
//  telephoneCheck("5555555");// should return false.
//  telephoneCheck("1 555)555-5555");// should return false.
//  telephoneCheck("1 555 555 5555");// should return true.
//  telephoneCheck("1 456 789 4444");// should return true.
//  telephoneCheck("123**&!!asdf#");// should return false.
//  telephoneCheck("55555555");// should return false.
//  telephoneCheck("(6054756961)");// should return false
//  telephoneCheck("2 (757) 622-7382");// should return false.
//  telephoneCheck("0 (757) 622-7382");// should return false.
//  telephoneCheck("-1 (757) 622-7382");// should return false
//  telephoneCheck("2 757 622-7382");// should return false.
//  telephoneCheck("10 (757) 622-7382");// should return false.
//  telephoneCheck("27576227382");// should return false.
//  telephoneCheck("(275)76227382");// should return false.
//  telephoneCheck("2(757)6227382");// should return false.
//  telephoneCheck("2(757)622-7382");// should return false.
//  telephoneCheck("555)-555-5555");// should return false.
//  telephoneCheck("(555-555-5555");// should return false.
//  telephoneCheck("(555)5(55?)-5555");// should return false.

To quote the MDN for match:
“If the string matches the expression, it will return an Array containing the entire matched string as the first element, followed by any results captured in parentheses. If there were no matches, null is returned.”

Also as an aside, checking for phone numbers is a ***** unless you put very specific structure on it. At least here they’ve limited themselves to what appears to be north american numbers.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.