# Telephone Number Validator help

**URL:** <https://forum.freecodecamp.org/t/telephone-number-validator-help/409256>\
**Category:** General\
**Created:** [July 12, 2020, 3:30pm UTC](https://forum.freecodecamp.org/t/telephone-number-validator-help/409256 "2020-07-12T15:30:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![reachsam002](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/reachsam002/32/143966_2.png) [@reachsam002](https://forum.freecodecamp.org/u/reachsam002)\
**Post date:** [July 12, 2020, 3:30pm UTC](https://forum.freecodecamp.org/t/telephone-number-validator-help/409256/1 "2020-07-12T15:30:19Z")

</div>

```auto
function telephoneCheck(str) {

var tel = /(\d{3}|\(\d{3}\)|1\s\d{3}|1\s\(\d{3}\)|1\(\d{3}\))(-\d{3}-|\s\d{3}-|\d{3}|\d{3}-|\s\d{3}\s)\d{4}/; 
if (tel.test(str) === true) {
  return true;
}
  else {
  return false
  };
}

telephoneCheck("555-555-5555");

```

How can I match the Area code which is 1

---

<div class="post-metadata">

**Author:** ![reachsam002](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/reachsam002/32/143966_2.png) [@reachsam002](https://forum.freecodecamp.org/u/reachsam002)\
**Post date:** [July 12, 2020, 3:47pm UTC](https://forum.freecodecamp.org/t/telephone-number-validator-help/409256/3 "2020-07-12T15:47:03Z")

</div>

```auto
function telephoneCheck(str) {

var tel = /(\d{3}|\(\d{3}\)|1\s\d{3}|1\s\(\d{3}\)|1\(\d{3}\))(-\d{3}-|\s\d{3}-|\d{3}|\d{3}-|\s\d{3}\s)\d{4}/; 
if (tel.test(str) === true) {
  return true;
}
  else {
  return false
  };
}

telephoneCheck("2 (757) 622-7382");

```

it should fail this but it returning true

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [July 12, 2020, 3:50pm UTC](https://forum.freecodecamp.org/t/telephone-number-validator-help/409256/4 "2020-07-12T15:50:11Z")

</div>

What have you tried so far to test for the `1` at the start?

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/match-beginning-string-patterns)**
>
> Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.

  

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/check-for-all-or-none)**
>
> Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.

More links

> **[Assertions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Assertions)**
>
> Assertions include boundaries, which indicate the beginnings and endings of lines and words, and other patterns indicating in some way that a match is possible (including look-ahead, look-behind, and conditional expressions).

  

> **[Quantifiers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Quantifiers)**
>
> Quantifiers indicate numbers of characters or expressions to match.

  

> **[Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript](https://regex101.com)**
>
> Regex101 allows you to create, debug, test and have your expressions explained for PHP, PCRE, Python, Golang and JavaScript. The website also features a community where you can share useful expressions.

---

<div class="post-metadata">

**Author:** ![anon10002461](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anon10002461](https://forum.freecodecamp.org/u/anon10002461)\
**Post date:** [July 12, 2020, 4:20pm UTC](https://forum.freecodecamp.org/t/telephone-number-validator-help/409256/5 "2020-07-12T16:20:36Z")

</div>

```auto
function telephoneCheck(str) {

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

tel.test(str)===true ? console.log("true") : false 

};

```

remove the console.log and replace by true after you’re happy w the result.

**Problem:** you have to use `^` and `$`.

* * *

**Edit:**

There are probably more thinks to fix that you can address.

---

<div class="post-metadata">

**Author:** ![reachsam002](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/reachsam002/32/143966_2.png) [@reachsam002](https://forum.freecodecamp.org/u/reachsam002)\
**Post date:** [July 12, 2020, 10:09pm UTC](https://forum.freecodecamp.org/t/telephone-number-validator-help/409256/6 "2020-07-12T22:09:31Z")

</div>

Thanks I have passed the challenge by stating the beginning with ^ and end with $

---

<div class="post-metadata">

**Author:** ![anon10002461](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anon10002461](https://forum.freecodecamp.org/u/anon10002461)\
**Post date:** [July 12, 2020, 10:18pm UTC](https://forum.freecodecamp.org/t/telephone-number-validator-help/409256/7 "2020-07-12T22:18:10Z")

</div>

Glad you passed. Remember that you can use grouping in this way too:` /(\d{3})-\1/`

And will be interpreted as ` /\d{3}-\d{3}/` but is way simpler to write.

---

<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:** [June 1, 2021, 6:47pm UTC](https://forum.freecodecamp.org/t/telephone-number-validator-help/409256/8 "2021-06-01T18:47:16Z")

</div>

Closed on June 1, 2021, 6:47pm UTC


