Hi everybody!
I’ve been working on the Telephone Number Validator and I’ve gotten myself a bit stuck. My strategy was originally to just remove all standard formatting tools in phone numbers (parentheses, spaces, dashes) and then check that everything in the number was digits. If they were all digits and 10 characters long, return true. If they were all digits, 11 characters long, and the first number was a 1, return true. Anything else is false.
However, I realized after that the formatting tools had to be in a certain place so my code didn’t reject some cases it should have. I’m fairly certain my entire problem is incorrect true returns, I didn’t notice any incorrect false returns.
Regex are a pretty big weak point for me. Does anybody know of any strong resources I could use to study them? I’m including my code below as well if anybody would like to give that a look and offer me some advice.
Thanks everybody!
/**
* telephoneNumberValidator.js
*
* Takes in a string and determines whether or not the numbers in it
* create a valid US Telephone Number.
*
* @author Chris Wolf
* @version December 11, 2018
* chriswolfdesign@gmail.com
*/
/**
* Takes in a string and determines whether or not he numbers in it create a
* valid US Telephone Number.
*
* @param {str} -- the original string passed in
*
* @return {boolean} true if the string represents a valid US Telephone Number,
* false otherwise
*/
function telephoneCheck(str) {
let newStr = str;
newStr = newStr.replace(/\s/g, "");
newStr = newStr.replace(/\(/g, "");
newStr = newStr.replace(/\)/g, "");
newStr = newStr.replace(/-/g, "");
for(var i = 0; i < newStr.length; i++) {
if(isNaN(parseInt(newStr[i]))) {
return false;
}
}
if(newStr.length === 10) {
return true;
}
if(newStr.length === 11 && newStr[0] === 1) {
return true;
}
return false;
}
console.log(telephoneCheck("555-555-5555"));
console.log(telephoneCheck("1 555-555-5555"));
console.log(telephoneCheck("1 (555) 555-5555"));
console.log(telephoneCheck("5555555555"));
console.log(telephoneCheck("555-555-5555"));
console.log(telephoneCheck("(555)555-5555"));
console.log(telephoneCheck("1(555)555-5555"));
console.log(telephoneCheck("555-5555"));
console.log(telephoneCheck("5555555"));
console.log(telephoneCheck("1 555)555-5555"));
console.log(telephoneCheck("1 555 555 5555"));
console.log(telephoneCheck("1 456 789 4444"));
console.log(telephoneCheck("123**&!!asdf#"));
console.log(telephoneCheck("55555555"));
console.log(telephoneCheck("(6054756961)"));
console.log(telephoneCheck("2 (757) 622-7382"));
console.log(telephoneCheck("0 (757) 622-7382"));
console.log(telephoneCheck("-1 (757) 622-7382"));
console.log(telephoneCheck("2 757 622-7382"));
console.log(telephoneCheck("10 (757) 622-7382"));
console.log(telephoneCheck("27576227382"));
console.log(telephoneCheck("(275)76227382"));
console.log(telephoneCheck("2(757)6227382"));
console.log(telephoneCheck("2(757)622-7382"));
console.log(telephoneCheck("555)-555-5555"));
console.log(telephoneCheck("(555-555-5555"));
console.log(telephoneCheck("(555)5(55?)-5555"));