hello good afternoon, I am in this part of the project where we have to validate a US phone number, try to make all the regular expression in 1 (it did not go well) so I tried to decide between the numbers that start with 1 and those that do not . but I still have a problem with this case (I know how to solve that particular case but another one would fail)
telephoneCheck(“1 555)555-5555”)` should return false.
I am thankful for any kind of help
function telephoneCheck(str) {
var regex1= /^([1])/
if(regex1.test(str)===true){
var regex2 = /^([1])\s*\(*[0-9]{3}\)*\-*\s*[0-9]{3}\)*\-*\s*[0-9]{4}$/
console.log(str.match(regex2))
return regex2.test(str)
}else if(regex1.test(str)===false){
var myRegex= /^(\([0-9]{3}\)|[0-9]{3})\s*\(*\-*[0-9]{3}\)*\-*\s*[0-9]{4}$/
return myRegex.test(str)
}
}
console.log(telephoneCheck("1 555)555-5555"));