# Phone number validator not working!

**URL:** <https://forum.freecodecamp.org/t/phone-number-validator-not-working/741698>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [March 31, 2025, 9:08pm UTC](https://forum.freecodecamp.org/t/phone-number-validator-not-working/741698 "2025-03-31T21:08:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![2012alexni](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/2012alexni/32/389034_2.png) [@2012alexni](https://forum.freecodecamp.org/u/2012alexni)\
**Post date:** [March 31, 2025, 9:08pm UTC](https://forum.freecodecamp.org/t/phone-number-validator-not-working/741698/1 "2025-03-31T21:08:29Z")

</div>

Step 35 and step 36 glitching, even when it’s working. Here’s the code:

```js
const userInput = document.getElementById('user-input');
const checkBtn = document.getElementById('check-btn');
const resultsDiv = document.getElementById('results-div');
const theclear = document.getElementById("clear-btn");

const pf1= /1 \([1-9][1-9][1-9]\) [1-9][1-9][1-9]-[1-9][1-9][1-9][1-9]/;
const pf2= /\([1-9][1-9][1-9]\)[1-9][1-9][1-9]-[1-9][1-9][1-9][1-9]/
const pf3= /[1-9][1-9][1-9]-[1-9][1-9][1-9]-[1-9][1-9][1-9][1-9]/;
const pf4= /[1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9]/;
const pf5= /1 [1-9][1-9][1-9] [1-9][1-9][1-9] [1-9][1-9][1-9][1-9]/;
const pf6= / 1\([1-9][1-9][1-9]\)[1-9][1-9][1-9]-[1-9][1-9][1-9][1-9]/;
const pf7= /1 [1-9][1-9][1-9]-[1-9][1-9][1-9]-[1-9][1-9][1-9][1-9]/;
const wrong= /^(?!1$)\d{2,} [1-9][1-9][1-9]-[1-9][1-9][1-9]-[1-9][1-9][1-9][1-9]/;
const wrong2= /\([1-9][1-9][1-9]-[1-9][1-9][1-9]-[1-9][1-9][1-9][1-9]/;
const wrong3= /^(?!1$)\d{1,}\([1-9][1-9][1-9]\)[1-9][1-9][1-9]-[1-9][1-9][1-9][1-9]/;
theclear.addEventListener("click", function(){
  resultsDiv.innerHTML = "";
});
checkBtn.addEventListener("click",function(){
  if(userInput.value===""){
    alert("Please provide a phone number");
  }else if(userInput.value.charAt(0)==="-"){
    resultsDiv.innerHTML="Invalid US number: "+userInput.value;
  }else if(pf1.test(userInput.value)||pf2.test(userInput.value)||pf3.test(userInput.value)||pf4.test(userInput.value)||pf5.test(userInput.value)||pf6.test(userInput.value)||pf7.test(userInput.value)){
    if(wrong.test(userInput.value)||wrong2.test(userInput.value)||wrong3.test(userInput.value)){
      resultsDiv.innerHTML="Invalid US number: "+userInput.value;
    }else{
      resultsDiv.innerHTML="Valid US number: "+ userInput.value;
    }
  }else{
    resultsDiv.innerHTML="Invalid US number: "+userInput.value;
  }
});
// Valids: 1 555-555-5555, 
//1(555) 555-5555, 5555555555, 555-555-5555, 
//(555)-555-5555, 1(555)555-5555
//, 1 555 555 5555, 1 456 789 4444.

```

---

<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:** [April 1, 2025, 9:54am UTC](https://forum.freecodecamp.org/t/phone-number-validator-not-working/741698/2 "2025-04-01T09:54:59Z")

</div>

can you share your html and a link to the project please?

* * *

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (').

---

<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:** [April 1, 2025, 6:55pm UTC](https://forum.freecodecamp.org/t/phone-number-validator-not-working/741698/3 "2025-04-01T18:55:06Z")

</div>

One of your “wrong” regex is matching on a valid number. Check them against the number from the failing test output. Copy and paste the number from the test so you get it with the correct spaces, or lack there of.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [October 1, 2025, 6:55am UTC](https://forum.freecodecamp.org/t/phone-number-validator-not-working/741698/4 "2025-10-01T06:55:21Z")

</div>

Closed on October 1, 2025, 6:55am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
