# Validate Phone numbers

**URL:** <https://forum.freecodecamp.org/t/validate-phone-numbers/126287>\
**Category:** General\
**Created:** [June 11, 2017, 11:58pm UTC](https://forum.freecodecamp.org/t/validate-phone-numbers/126287 "2017-06-11T23:58:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![smmoon2](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@smmoon2](https://forum.freecodecamp.org/u/smmoon2)\
**Post date:** [June 11, 2017, 11:58pm UTC](https://forum.freecodecamp.org/t/validate-phone-numbers/126287/1 "2017-06-11T23:58:26Z")

</div>

I’m really close to the validate phone numbers solution. However, I’m wondering why I’m getting tripped up on the parentheses… I’m checking my regex with this site: [http://www.regexpal.com/](http://www.regexpal.com/)  
Basically, my regex is not catching if the parentheses are in the right spot or not.

```auto
function telephoneCheck(str) {
  var re = /[^0-9 )(-]+/g;
  var re2 = /[0-9]/g;
  if (str.match(re)) {
    return false;
  }

  if (str.match(re2).length > 11 || str.match(re2).length < 10) {
    return false;
  }
  if (str.match(re2).length === 11 && str[0] !== "1") {
    return false;
  }
  var whole1 = /1?[-(]|\s?\d{3}[-)]|\s?\d{3}-|\s?\d{4}/g;
  if (str.match(whole1)) {
    return true;
  } 
}

```

Why does my code allow such things as

```auto
(5551239900)

```

or

```auto
213)4550018

```

Is there a way to say “only match the parentheses if there are open and closed and they are in the right spot”?

---

<div class="post-metadata">

**Author:** ![j0hnc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/j0hnc/32/57505_2.png) [@j0hnc](https://forum.freecodecamp.org/u/j0hnc)\
**Post date:** [June 12, 2017, 5:07am UTC](https://forum.freecodecamp.org/t/validate-phone-numbers/126287/2 "2017-06-12T05:07:09Z")

</div>

use a regex conditional [http://www.rexegg.com/regex-conditionals.html#syntax](http://www.rexegg.com/regex-conditionals.html#syntax)

---

<div class="post-metadata">

**Author:** ![smmoon2](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@smmoon2](https://forum.freecodecamp.org/u/smmoon2)\
**Post date:** [June 12, 2017, 5:29pm UTC](https://forum.freecodecamp.org/t/validate-phone-numbers/126287/3 "2017-06-12T17:29:10Z")

</div>

Thanks for the response! My problem is I am using conditionals already, but I think I need to make a conditional within a conditional. My regex currently:

```auto
/1?[-(]|\s?\d{3}[-)]|\s?\d{3}-|\s?\d{4}/g

```

I have 1? (start with a 1 or nothing), followed by [-(]|\s? (then have an open parenthesis, hyphen, whitespace, or nothing), then my three digits, then closed parenthesis, hyphen, whitespace, or nothing.

How do I set up to require both or no parenthesis? I tried:

```auto
(?[-(]|\s)?

```

but that is invalid… How do I say “If open parenthesis, then 3 digits, followed by closed parenthesis, or just three digits?”

---

<div class="post-metadata">

**Author:** ![j0hnc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/j0hnc/32/57505_2.png) [@j0hnc](https://forum.freecodecamp.org/u/j0hnc)\
**Post date:** [June 13, 2017, 1:27am UTC](https://forum.freecodecamp.org/t/validate-phone-numbers/126287/4 "2017-06-13T01:27:44Z")

</div>

Did you solve it already?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:55pm UTC](https://forum.freecodecamp.org/t/validate-phone-numbers/126287/5 "2021-01-17T20:55:57Z")

</div>

Closed on January 17, 2021, 8:55pm UTC


