Build a Telephone Number Validator Project - Build a Telephone Number Validator

Tell us what’s happening:

So according to the instruction this was suppose to work but instead it gives me an error maybe my regex is wrong or what please help?

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Your Title Here</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <h1>Hello, World!</h1>

    
    <input type="text" id="user-input">

    
    <button id="check-btn">Check</button>
    <button id="clear-btn">Clear</button>

    
    <div id="results-div"></div>

    <script src="./script.js"></script>
</body>
</html>

/* file: styles.css */

/* file: script.js */
const input = document.getElementById("user");
const check = document.getElementById("check-btn");
const clear = document.getElementById("clear-btn");
const results = document.getElementById("results-div");

check.addEventListener("click", () =>{
  const regex = /^(1\s?)?(\(\d{3}\)|\d{3})([\s-]?)\d{3}([\s-]?)\d{4}$/

  if (!input){
    alert("Please provide a phone number")
  }else if(regex.test(input)){
    results.innerHTML = `Valid US number:  ${input}`
  }else{
    results.innerHTML = `Invalid US number:  ${input}`
  }
});

clear.addEventListener("click", () =>{
  results.innerHTML = null;
})





Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

Build a Telephone Number Validator Project - Build a Telephone Number Validator

Hi @davidmoshe110

You are not accessing input from the user.

Try console.log to see what each of the variables look like.

Happy coding

the first issue you have is this two lines

do you have an element with that id?
and how do you check if the input is empty?

Thanks for spotting this one out :sparkling_heart:

Thanks it solved the problem

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