Skip to content

Instantly share code, notes, and snippets.

@sashagra
Last active February 28, 2021 16:16
Show Gist options
  • Save sashagra/cae5476be81ea3ebf771ae5ac263e90d to your computer and use it in GitHub Desktop.
Save sashagra/cae5476be81ea3ebf771ae5ac263e90d to your computer and use it in GitHub Desktop.
my own regexp to check phone number
function telephoneCheck(str) {
const regexp = /^(1\s*\(\d{3}\)|1\s\d{3}|\(\d{3}\)|\d{3})(\s|-)*\d{3}(\s|-)*\d{4}$/
return regexp.test(str)
}
const arr = [
"1 555)555-5555", // should return false.
"2 (757) 622-7382", // should return false.
"0 (757) 622-7382", // should return false.
"-1 (757) 622-7382", // should return false
"2 757 622-7382", // should return false.
"10 (757) 622-7382", // should return false.
"27576227382", // should return false.
"2(757)6227382", // should return false.
"2(757)622-7382", // should return false.
"555)-555-5555", // should return false.
"(555-555-5555", // should return false.
'555-555-5555', // should return true.
'(555)555-5555', // should return true.
'(555) 555-5555', // should return true.
'555 555 5555', // should return true.
'5555555555', // should return true.
'1 555 555 5555' // should return true.
]
for (let num of arr) {
console.log(telephoneCheck(num), num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment