Telephone Number Validator Project

The issue I am currently having is that when I do type in a valid number like 555-555-5555, the result would say that it is an invalid number thus not allowing me to past the rest of the tests.

I was also wondering why my code doesn’t show multiple results on the phone instead it rewrites the first result shown, thus making the clear button unnecessary

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Telephone Number Validator</title>
  <link rel="stylesheet" href="styles.css">
</head>

<body>
  <main>
    <img class="logo" src="https://cdn.freecodecamp.org/platform/universal/fcc_primary.svg" alt="freeCodeCamp Logo"/>
    <h1>Telephone Number Validator</h1>
    <div class="phone-container">
      <div class="phone-background">
        <div class="phone-camera"></div>
      </div>
      <label>Enter a Phone Number:</label>
      <input id="user-input" type="text">
      <div id="results-div"></div>
      <div class="phone-base">
        <button class="btn-style" id="check-btn">Check</button>
        <button class="btn-style" id="clear-btn">Clear</button>
      </div>
    </div>
  </main>
  <script src="script.js"></script>
</body>

</html>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body{
  background-color:#3B3B4F;
  font-family: Verdana, sans-serif;
  color:#0a0a23;
}

main{
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding:40px 10px;
}

.logo{
  width:100%;
  height:30px;
  margin-bottom:20px;
}

h1{
  color:white;
  width:100%;
  max-width:450px;
  text-align:center;
  margin:15px;
}

.phone-container{
  position:relative;
  width:250px;
  height:460px;
  border:15px solid black;
  border-radius:15px;
  background-color:#DFDFE2;
  margin:30px auto;
  display: flex;
  flex-direction:column;
  align-items:center;
}

.phone-background{
  background-color:black;
  width:100%;
  height:25px;
}

.phone-camera{
  background-color: #DFDFE2;
  width:10px;
  height:10px;
  border-radius:50%;
  margin:auto;
}

label{
  margin:12px;
}

#user-input{
  border:1px solid black;
  border-radius: 10px;
  height:45px;
  width:200px;
  text-align:center;
  margin:5px;
}

.phone-base{
  background-color:black;
  width:100%;
  height:40px;
  position:absolute;
  bottom:0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-style{
  cursor:pointer;
  width:100px;
  margin:10px;
  font-size:18px;
  background-image:linear-gradient(white,#928d86);
  border-color:white;
  border-width:3px;
}

#results-div{
  font-size: 1.2rem;
  padding: 7px;
  text-align: center;
  margin: 10px 0;
}
const userInput = document.getElementById('user-input');
const checkBtn = document.getElementById('check-btn');
const clearBtn = document.getElementById('clear-btn');
const result = document.getElementById('results-div')

//Declaring a regular expression which matches all valid formats
function numCheck(str){
  const regex = /^1?\s?(\d{3}|\(\d{3}\))-?\s?\d{3}-?\s?\d{4}/gm
  return regex.test(str);
};

function numInput(){
if (userInput.value === ""){
  alert('Please provide a phone number');
} else if(userInput.value === numCheck){
  result.innerHTML = 'Valid US number: ' + userInput.value;
} else{
  result.innerHTML = 'Invalid US number: ' + userInput.value;
}
userInput.value =''
};

const refresh = () => {
  result.innerHTML = '';
};

checkBtn.addEventListener('click',numInput)
clearBtn.addEventListener('click',refresh)

Can you post the link page pls?

you wrote your code so that whatever was the previous result it is overwritten

are you sure this is the way you pass a value to the function numCheck?

1 Like
else if (numCheck(userInput.value)

This allowed me to finally get valid number results but I am unsure how i prevent the result from being overwritten and thank you for your assistance