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

Hey all! Congrats on completing the fourth project in this bootcamp :tada:

Let’s post our solutions! I’ll start with mine:

function telephoneCheck(str) {
  // Define several regular expressions to match US phone number
  const regex1 = /^(1\s)?\d{3}-\d{3}-\d{4}$/;
  const regex2 = /^(1(\s)?)?\(\d{3}\)(\s)?\d{3}-\d{4}$/;
  const regex3 = /^(1\s)?\d{3}\s\d{3}\s\d{4}$/;
  const regex4 = /^\d{10}$/;

  const regexes = [regex1, regex2, regex3, regex4];

  // loop through all regexes
  for (let regex of regexes) {
    // if one of them matches the phone number
    if (regex.test(str)) {
      return true
    }
  }

  // none of them matched, return false
  return false;
}

console.log(telephoneCheck("1(555)555-5555"));
3 Likes

Interesting class today.

Here is my solution:

function telephoneCheck(str) {
  let regExp = /^(1\s?)?(\d{3}|\(\d{3}\))[\-\s]?\d{3}[\-\s]?\d{4}$/
  return regExp.test(str);
}

console.log(telephoneCheck("1 (555) 555-5555"));
2 Likes

Here’s mine:

function telephoneCheck(str) {

  // format is 5555555555 or 15555555555
  let formatOne = /^1?\s?\d{10}$/;

  // format is 555 555 5555 or 1 555 555 5555
  let formatTwo = /^1?\s?\d{3}\s\d{3}\s\d{4}$/;
  
  // format is 555-555-5555 or 1 555-555-5555
  let formatThree = /^1?\s?\d{3}-\d{3}-\d{4}$/;

  // format is (555) 555-5555 or 1 (555) 555-5555 or (555)555-5555 or 1(555)555-5555
  let formatFour = /^1?\s?\(\d{3}\)\s?\d{3}-\d{4}$/;


if (formatOne.test(str) || formatTwo.test(str) || formatThree.test(str) || formatFour.test(str)) {
  return true;
} else {
  return false;
}

}

console.log(telephoneCheck("1(555)555-5555"));
2 Likes

Hi, at the moment I am in absolute tears of frustration. I started this exercise about a week ago and could find no easier way to do it than the way Ramon started this morning. I have watched the video up to the point where Ramon starts to tidy his code up a little.

My code is pretty much identical to Ramon’s. But I have a problem I am unable to get past. A pointer would be appreciated. Please don’t tell me I can do this in one line - I get that, but I don’t get this!!!

function telephoneCheck(str) {
 const regex1 = /^\d{3}-\d{3}-\d{4}$/;
 const regex2 = /^\(\d{3}\)\d{3}-\d{4}$/;
 const regex3 = /^\(\d{3}\)\s\d{3}-\d{4}$/;
 const regex4 = /^\d{3}\s\d{3}\s\d{4}$/;
 const regex5 = /^\d{10}$/;
 const regex6 = /^1\s\d{3}\s\d{3}\s\d{4}$/;
 const regex7 = /^1\s\d{3}-\d{3}-\d{4}$/;
 const regex8 = /^1\s\(\d{3}\)\s\d{3}-\d{4}/;
 
 const regexes = [regex1, regex2, regex3, regex4, regex5, regex6, regex7, regex8];

 for (let regex of regexes){
  if (regex.test(str)){
    return true;
  }else{
    return false;
  }
} 
  return true;
}

console.log(telephoneCheck("1 555-555-5555"));

I think it looks OK, I think it should work but…

false

I don’t know why it is false! I cannot see what I am doing wrong

 const regex6 = /^1\s\d{3}\s\d{3}\s\d{4}$/;
 //const regex7 = /^1\s\d{3}-\d{3}-\d{4}$/;
 const regex8 = /^1\s\(\d{3}\)\s\d{3}-\d{4}/;
 
// const regexes = [regex1, regex2, regex3, regex4, regex5, regex6, regex7, regex8];

 const regex7 = /^1\s\d{3}-\d{3}-\d{4}$/;
 const regexes = [regex7] 

You can see what I have greyed out and you can see what I have added in. regex7 is the same - there is no change, they are copies of each other. So why am I now getting…

true

And exactly the same works for regex8.
If I take out regex 7 and 8 and combine them with earlier ones using (1\s)? the earlier ones stop working!!!

I have been working on this for 4 1/2 hours today and I am frustrated. If someone can explain what it is I am doing wrong I would be most grateful

1 Like

Sorry, for some reason I have an extra return true in my code but taking it out doesn’t make any difference to the problem I have.

Hey William!

So sorry this has been frustrating.

See the code I highlighted? This is returning false immediately.

I recommend plugging your code in here to watch it move visually:
https://pythontutor.com/javascript.html#mode=edit

You’ll see that when trying out the first regex, if it doesn’t match, the function will immediately return false.

Maybe try removing that else statement and see if it works :slight_smile:

If not, maybe your function is always returning true. If none of the regexes match, it should return false.

Good luck!

1 Like

Hi everyone

This my first solution to this Project, but I will back on it in a few days to keep learning more about regex and improve readability and code.

I have commented all my code, note that all my test are there, and you could find them useful.

function telephoneCheck(str) {

  const REGEX = /^(1[\s-]|1)?(\((?=\d{3}\))\d{3}(?<=\(\d{3})\)|\d{3})[\s-]?\d{3}[\s-]?\d{4}$/;
  
// ^(1[\s-]|1)? --> Starts with number 1 followed by space or hyphen -, or just 1
// (\((?=\d{3}\)) --> match open parentheses (, ONLY is followed by 3 digits and close parentheses ).
// \d{3} --> match 3 digits (\d)
// (?<=\(\d{3})\) --> match close parentheses ) ONLY is precedeed by open parentheses ( and 3 digits. 
// [\s-]? match a space (\s) or a hyphen (-), or just nothing at all
// \d{4}$ finish with 4 digits

  // Used to making tests every step, commented at the end of the process
  // return str.match(REGEX); 

  return REGEX.test(str);
}

telephoneCheck("555-555-5555");

// ALL TESTS: testing with return str.match(REGEX); if every chunk of code works 

// TRUE
// console.log(telephoneCheck("1 555-555-5555"));
// console.log(telephoneCheck("1 (555) 555-5555"));
// console.log(telephoneCheck("555-555-5555"));
// console.log(telephoneCheck("5555555555"));
// console.log(telephoneCheck("(555)555-5555"));
// console.log(telephoneCheck("1(555)555-5555"));
// console.log(telephoneCheck("1 555 555 5555"));
// console.log(telephoneCheck("1 456 789 4444"));
// console.log(telephoneCheck("5555555555"));

// FALSE
// console.log(telephoneCheck("5555555"));
// console.log(telephoneCheck("123**&!!asdf#"));
// console.log(telephoneCheck("55555555"));
// console.log(telephoneCheck("(6054756961)"));
// console.log(telephoneCheck("2 (757) 622-7382"));
// console.log(telephoneCheck("-1 (757) 622-7382"));
// console.log(telephoneCheck("10 (757) 622-7382"));
// console.log(telephoneCheck("555)-555-5555"));
// console.log(telephoneCheck("(555-555-5555"));

You might like to check this links too:

Regular expressions(Regular expressions - JavaScript | MDN), check the tools at the end of the article. :smile:

RegExp object (RegExp - JavaScript | MDN)

Regular expression syntax cheatsheet(Regular expression syntax cheatsheet - JavaScript | MDN)

I hope this helps. Happy coding all!

3 Likes

Thank you so much Ramon. I do use pythontutor on every bit of code I write but it wasn’t showing me what I was doing wrong. I knew I had to have a return false somewhere in order to deal with all the incorrect versions and it just seemed to me that the obvious place was after the if statement. I don’t think I really realized that the program wasn’t testing every condition.

So I moved it and yes, it worked, as for tidying the code up - maybe tomorrow! Now it is wine o’clock. Thank you again for your help!

2 Likes

Here is mine! Much improved from yesterday, after following today’s class.
Note I used both an overall | for the regex as well as a “minor or” within a small portion of the code to select between space or dash if present:

function telephoneCheck(str) {

  let numRegex = /^(1(\s)?)?\d{3}(\s|\-)?\d{3}(\s|\-)?\d{4}$|^(1(\s)?)?\(\d{3}\)(\s|\-)?\d{3}(\s|\-)?\d{4}$/;

 return numRegex.test(str);
 

}

console.log(telephoneCheck("1 555-555-5555"));
2 Likes
function telephoneCheck(str) {
  const regex = /^\d{3}[- ]?\d{3}[- ]?\d{4}$|^\(\d{3}\)[ -]?\d{3}[ -]?\d{4}$/;
  const regexPlusLand = /^1 ?\d{3}[- ]?\d{3}[- ]?\d{4}$|^1 ?\(\d{3}\)[ -]?\d{3}[ -]?\d{4}$/
  return regexNumber.test(str) || regexPlusLand.test(str);
}

I did it right after the regex-chapter, somehow I couldn’t find the way to make 1 at the beginning optional, but too lazy to change it now

3 Likes

const telephoneCheck = str => {
const regExp = /^1?\s?((\d{3})|\d{3})(\s|-)?\d{3}(\s|-)?\d{4}$/gm
return regExp.test(str)
}

telephoneCheck(“27576227382”);

		/*			Regular Expression Explanation Below !!! 			*/

/*
Expression : /^1?\s?((\d{3})|\d{3})(\s|-)?\d{3}(\s|-)?\d{4}$/gm

^ asserts position at start of a line
1? matches the character 1 literally (case sensitive)
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
\s? matches any whitespace character (equal to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
1st Capturing Group (\(\d{3}\)|\d{3})
1st Alternative \(\d{3}\)
\( matches the character ( literally (case sensitive)
\d{3} matches a digit (equal to [0-9])
{3} Quantifier — Matches exactly 3 times
\) matches the character ) literally (case sensitive)
2nd Alternative \d{3}
\d{3} matches a digit (equal to [0-9])
{3} Quantifier — Matches exactly 3 times
2nd Capturing Group (\s|-)?
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative \s
\s matches any whitespace character (equal to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
2nd Alternative -
- matches the character - literally (case sensitive)
\d{3} matches a digit (equal to [0-9])
{3} Quantifier — Matches exactly 3 times
3rd Capturing Group (\s|-)?
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative \s
\s matches any whitespace character (equal to [\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
2nd Alternative -
\d{4} matches a digit (equal to [0-9])
{4} Quantifier — Matches exactly 4 times
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

*/

3 Likes

Regex usually makes me want to tear my hair out in frustration so I’m (pleasantly!) surprised that I was able to come up with a solution for this one. I’m a little disappointed I couldn’t figure out how to test it with a single regex like some of y’all did, but overall I’m still happy with it.

function telephoneCheck(str) {
  let checkForParentheses = /[()]/g
  if (checkForParentheses.test(str)) {
    let regex = /^1?\s?\(\d{3}\)\s?\d{3}[-\s]?\d{4}$/
    return (regex.test(str))
  }
  let regex = /^1?[-\s]?\d{3}[-\s]?\d{3}[-\s]?\d{4}$/
  return (regex.test(str))
}
4 Likes

So after my huge frustration of yesterday this is my final code:

function telephoneCheck(str) {

if (/^(1|1\s)?(\(\d{3}\)|\d{3})(-|\s)?\d{3}(-|\s)?\d{4}$/.test(str)){return true}
/* the number should be, from the start
1 or 1 followed by space or neither
followed by 3 digits in brackets or not in brackets
followed by hyphen or whitespace or neither
followed by 3 digits
followed by hyphen or whitespace or neither
followed by 4 digits to the end to return true, otherwise it should return false*/ 


return false
}

console.log(telephoneCheck("1 (555) 555-5555"));
6 Likes

I shared mine yesterday during the lesson. I hardly build regex’s from scratch, but this one was doable. Eternally grateful for regex101.com and its debugger :slight_smile:

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

console.log(telephoneCheck("555-555-5555"));
console.log(telephoneCheck("555-555-5555"));
console.log(telephoneCheck("(555)555-5555"));
console.log(telephoneCheck("(555) 555-5555"));
console.log(telephoneCheck("555 555 5555"));
console.log(telephoneCheck("5555555555"));
console.log(telephoneCheck("1 555 555 5555"));
4 Likes

Here is one of my solutions to the Telephone Number Validator.
Cheers, T.


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

console.log(telephoneCheck("800-420-1234"));
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"));
console.log(telephoneCheck("55 55-55-555-5"));
console.log(telephoneCheck("11 555-555-5555"));
3 Likes

I am left with only two unresolved solutions, What am I missing here ?

function telephoneCheck(str) {
return (str.match(/^(1\s?)[(]?(\d{3})[)]?(-?|\s)?(\d{3})(-?|\s)?(\d{4})$|^(1\s)?(\d{3})-(\d{3})-(\d{4})$|^(\d{10})$|^(\d{10})$/)) ? true : false
}
console.log(telephoneCheck("(555)555-5555"))

2 Likes

Hola Damián @damian2665429

At least in the first part ^(1\s?)? you are missing an ?:

function telephoneCheck(str) {

const regex = /^(1\s?)?[(]?(\d{3})[)]?(-?|\s)?(\d{3})(-?|\s)?(\d{4})$|^(1\s)?(\d{3})-(\d{3})-(\d{4})$|^(\d{10})$|^(\d{10})$/;

console.log(str.match(regex));

return regex.test(str);

}

// console.log(telephoneCheck("1 555)555-5555")) //false
// console.log(telephoneCheck("555)-555-5555")) //false
// console.log(telephoneCheck("(555-555-5555")) //false

I console.log() only test that doesn’t work now.

I think you are forgetting the fact that if there is a ( should be followed by 3 digits and ). If you want to do this way you might need to look this Other assertions.

You can check my solution here, I have used those.

Sorry, I need to go, hope that helps.

Happy coding and weekend!

3 Likes

Thank you! The issued lied in the fact that I did not know how to explicitly match parenthesis. Anyways, here is my solution! thanks a lot!

function telephoneCheck(str) {

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

}
4 Likes
function telephoneCheck(str) {
  const validNumberPatterns = [
    // 555-555-5555
    /^\d{3}-\d{3}-\d{4}$/,
    // (555)555-5555
    /^\(\d{3}\)\d{3}-\d{4}$/,
    // (555) 555-5555
    /^\(\d{3}\) \d{3} \d{4}$/,
    // 555 555 5555
    /^\d{3} \d{3} \d{4}$/,
    // 5555555555
    /^\d{10}$/,
    // 1 555 555 5555
    /^1 \d{3} \d{3} \d{4}$/,
    // 1 555-555-5555
    /^1 \d{3}-\d{3}-\d{4}$/,
    // 1 (555) 555-5555
    /^1 \(\d{3}\) \d{3}-\d{4}$/,
    // 1(555)555-5555
    /^1\(\d{3}\)\d{3}-\d{4}$/
  ];

  return validNumberPatterns
    .some((pattern) => pattern.test(str));
}

console.log(telephoneCheck("2(757)622-7382")); // false
console.log(telephoneCheck("-1 (757) 622-7382")); // false

telephoneCheck("555-555-5555"); // true

3 Likes

I find regex difficult to read as it starts getting long. I had written a regex match/phone number format before to display a phone number coming from already formatted API data. So, I started this challenge googling to expand on what I’d written and ran across this video: [Telephone Validator - Project 4 Javascript Certification FreeCodeCamp - YouTube](telephone validator video)

I found the video super helpful to understand regex for this challenge. Sharing the link as it helped me get to the above solution as I started going through the test examples.

3 Likes