Post your Telephone Number Validator solutions here! (Fall 2022)

That’s a nice solution, making use of the array method some.

Your solution otherwise follows largely Ramon’s approach, as it utilizes many simpler regex expressions to validate a number. You should definitely try to optimize them a bit to make it more concise, without losing readability. Example: testing for the optional 1 with a ? would cut the number of patterns by 50% already.

Thanks for posting your solution!

2 Likes

ngl this one annoyed the hell out of me, felt more like whack a mole than a phone number validator with the brackets and dashes included

function telephoneCheck(str) {
  const regex = /^1?\d{10}$|^1?\s?\(\d{3}\)\s?\d{3}-\d{4}|^1?(|\s)\d{3}(-|\s)\d{3}(\s|-)\d{4}/;
  
  return regex.test(str);
}

telephoneCheck("555-555-5555");
1 Like

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