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

**URL:** <https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925>\
**Category:** JavaScript Bootcamp\
**Created:** [October 5, 2022, 8:37am UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925 "2022-10-05T08:37:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hola\_soy\_milk](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/hola_soy_milk/32/600_2.png) [@hola\_soy\_milk](https://group.classcentral.com/u/hola_soy_milk)\
**Post date:** [October 5, 2022, 8:37am UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/1 "2022-10-05T08:37:01Z")

</div>

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

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

```javascript
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"));

```

---

<div class="post-metadata">

**Author:** ![Timiphil](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/timiphil/32/5367_2.png) [@Timiphil](https://group.classcentral.com/u/Timiphil)\
**Post date:** [October 5, 2022, 8:51am UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/2 "2022-10-05T08:51:18Z")

</div>

Interesting class today.

Here is my solution:

```auto
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"));

```

---

<div class="post-metadata">

**Author:** ![whitr](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@whitr](https://group.classcentral.com/u/whitr)\
**Post date:** [October 5, 2022, 11:35am UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/3 "2022-10-05T11:35:17Z")

</div>

Here’s mine:

```auto
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"));

```

---

<div class="post-metadata">

**Author:** ![williambradle2950093](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/williambradle2950093/32/6027_2.png) [@williambradle2950093](https://group.classcentral.com/u/williambradle2950093)\
**Post date:** [October 5, 2022, 1:07pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/4 "2022-10-05T13:07:56Z")

</div>

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!!!

```auto
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

```auto
 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

---

<div class="post-metadata">

**Author:** ![williambradle2950093](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/williambradle2950093/32/6027_2.png) [@williambradle2950093](https://group.classcentral.com/u/williambradle2950093)\
**Post date:** [October 5, 2022, 1:14pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/5 "2022-10-05T13:14:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hola\_soy\_milk](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/hola_soy_milk/32/600_2.png) [@hola\_soy\_milk](https://group.classcentral.com/u/hola_soy_milk)\
**Post date:** [October 5, 2022, 1:20pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/6 "2022-10-05T13:20:11Z")

</div>

> [@williambradle2950093](#):
>
> ```auto
> }else{
> return false;
> }
> 
> ```

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](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 🙂

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

Good luck!

---

<div class="post-metadata">

**Author:** ![carlost2672543](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/carlost2672543/32/4162_2.png) [@carlost2672543](https://group.classcentral.com/u/carlost2672543)\
**Post date:** [October 5, 2022, 1:23pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/7 "2022-10-05T13:23:52Z")

</div>

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.

```auto
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions)), check the tools at the end of the article. 😄

# RegExp object ([RegExp - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp))

# Regular expression syntax cheatsheet([Regular expression syntax cheat sheet - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet))

I hope this helps. Happy coding all!

---

<div class="post-metadata">

**Author:** ![williambradle2950093](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/williambradle2950093/32/6027_2.png) [@williambradle2950093](https://group.classcentral.com/u/williambradle2950093)\
**Post date:** [October 5, 2022, 1:40pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/8 "2022-10-05T13:40:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Jane2963637](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/jane2963637/32/6117_2.png) [@Jane2963637](https://group.classcentral.com/u/Jane2963637)\
**Post date:** [October 5, 2022, 2:58pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/9 "2022-10-05T14:58:14Z")

</div>

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:

```auto
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"));

```

---

<div class="post-metadata">

**Author:** ![Tzerio](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/tzerio/32/6142_2.png) [@Tzerio](https://group.classcentral.com/u/Tzerio)\
**Post date:** [October 5, 2022, 7:23pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/10 "2022-10-05T19:23:11Z")

</div>

```auto
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

---

<div class="post-metadata">

**Author:** ![aliabukahil2922198](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/aliabukahil2922198/32/6535_2.png) [@aliabukahil2922198](https://group.classcentral.com/u/aliabukahil2922198)\
**Post date:** [October 5, 2022, 10:47pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/11 "2022-10-05T22:47:24Z")

</div>

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)

```

\*/

---

<div class="post-metadata">

**Author:** ![jazznichole](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/jazznichole/32/5802_2.png) [@jazznichole](https://group.classcentral.com/u/jazznichole)\
**Post date:** [October 6, 2022, 4:27am UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/12 "2022-10-06T04:27:36Z")

</div>

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.

```auto
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))
}

```

---

<div class="post-metadata">

**Author:** ![williambradle2950093](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/williambradle2950093/32/6027_2.png) [@williambradle2950093](https://group.classcentral.com/u/williambradle2950093)\
**Post date:** [October 6, 2022, 7:55am UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/13 "2022-10-06T07:55:32Z")

</div>

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

```auto
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"));

```

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [October 6, 2022, 9:25am UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/14 "2022-10-06T09:25:42Z")

</div>

I shared mine yesterday during the lesson. I hardly build regex’s from scratch, but this one was doable. Eternally grateful for [regex101.com](http://regex101.com) and its debugger 🙂

```auto
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"));

```

---

<div class="post-metadata">

**Author:** ![tusharmunge2610775](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/tusharmunge2610775/32/5939_2.png) [@tusharmunge2610775](https://group.classcentral.com/u/tusharmunge2610775)\
**Post date:** [October 6, 2022, 7:29pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/15 "2022-10-06T19:29:59Z")

</div>

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

```auto

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"));

```

---

<div class="post-metadata">

**Author:** ![damian2665429](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/damian2665429/32/2314_2.png) [@damian2665429](https://group.classcentral.com/u/damian2665429)\
**Post date:** [October 7, 2022, 6:13pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/16 "2022-10-07T18:13:52Z")

</div>

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

```auto
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"))

```

---

<div class="post-metadata">

**Author:** ![carlost2672543](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/carlost2672543/32/4162_2.png) [@carlost2672543](https://group.classcentral.com/u/carlost2672543)\
**Post date:** [October 7, 2022, 7:36pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/17 "2022-10-07T19:36:58Z")

</div>

Hola Damián @damian2665429

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

```auto
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet#other_assertions).

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

Sorry, I need to go, hope that helps.

Happy coding and weekend!

---

<div class="post-metadata">

**Author:** ![damian2665429](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/damian2665429/32/2314_2.png) [@damian2665429](https://group.classcentral.com/u/damian2665429)\
**Post date:** [October 9, 2022, 1:49pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/18 "2022-10-09T13:49:37Z")

</div>

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!

```auto
function telephoneCheck(str) {

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

}

```

---

<div class="post-metadata">

**Author:** ![awoodworth2946160](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@awoodworth2946160](https://group.classcentral.com/u/awoodworth2946160)\
**Post date:** [October 9, 2022, 8:37pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/19 "2022-10-09T20:37:22Z")

</div>

```auto
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

```

---

<div class="post-metadata">

**Author:** ![awoodworth2946160](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@awoodworth2946160](https://group.classcentral.com/u/awoodworth2946160)\
**Post date:** [October 9, 2022, 8:57pm UTC](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925/20 "2022-10-09T20:57:01Z")

</div>

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](https://www.youtube.com/watch?v=t6Yr2PBmwC0)](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.

[Next page](https://group.classcentral.com/t/post-your-telephone-number-validator-solutions-here-fall-2022/35925.md?page=2)
