Last active
December 27, 2019 02:30
-
-
Save JaymesKat/0ad5dde5a50e732212f844b7570d0c1b to your computer and use it in GitHub Desktop.
A function that returns true if the passed string looks like a valid US phone number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function telephoneCheck(str) { | |
if(!parenthesesBalanced(str)){ | |
return false; | |
} | |
const regex = /^1?\s*(\(?\d{3}\)?[-]?\s*){2}\d{4}$/ | |
return regex.test(str); | |
} | |
function parenthesesBalanced(str){ | |
const openBracketRegex = /\(/g | |
const closeBracketRegex = /\)/g | |
let result1 = str.match(openBracketRegex) | |
let result2 = str.match(closeBracketRegex) | |
if(!result1){ | |
result1 = [] | |
} | |
if(!result2){ | |
result2 = [] | |
} | |
return result1.length == result2.length | |
} | |
telephoneCheck("555-555-5555"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment