Tell us what’s happening:
I cannot seem to pass all of the tests for this project despite the output seeming to be correct. I pass the first test:
" When the #user-input
element contains 1 555-555-5555
and the #check-btn
element is clicked, the #results-div
element should contain the text Valid US number: 1 555-555-5555
."
but the remaining tests fail. I have tested my regex with every test phone number in the tests, they all act accordingly. I have modified this line:
output.innerHTML += <h2 class="output-text">${el}</h2>
;
to either
output.innerText += el;
OR
output.textContent += el;
with no success. I can’t find the problem with my output.
Thank you for your time.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href = "styles.css">
<title>US Telephone number validator</title>
</head>
<body>
<main>
<h1 id="title">US Telephone number validator</h1>
<div class="phone-container">
<div class="phone-header">
<div class="phone-camera"></div>
</div>
<div class="phone-screen">
<label for="user-input">Enter a phone number</label>
<input id="user-input" type="text"/>
<div id="results-div">
</div>
</div>
<div class="phone-footer">
<button id="check-btn">Check</button>
<button id="clear-btn">Clear</button>
</div>
</div>
</main>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
:root {
--dark-grey: #1a1a1a;
--grey: #4d4d4d;
--light-grey: #d9d9d9;
}
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: var(--grey);
color: white;
font-family: tahoma;
}
main {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#title {
margin: 50px;
text-align: center;
}
.phone-container {
height: 500px;
width: 300px;
background-color: var(--dark-grey);
border-radius: 20px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.phone-header {
height: 30px;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
}
.phone-camera {
height: 20px;
width: 20px;
border-radius: 50%;
background-color: var(--grey);
}
.phone-screen {
color: var(--dark-grey);
height: 400px;
width: 90%;
background-color: var(--light-grey);
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
}
label {
margin: 15px;
font-size: 1.5rem;
}
#user-input {
height: 3rem;
font-size: 1.5rem;
width: 80%;
margin-bottom: 50px;
}
#results-div {
height: 200px;
width: 80%;
overflow-y: auto;
}
.phone-footer {
height: 50px;
width: 100%;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
#check-btn, #clear-btn {
height: 70%;
width: 100px;
margin: 10px;
}
.output-text {
text-align: center;
margin-bottom: 10px;
}
/* file: script.js */
const userInput = document.getElementById("user-input");
const checkBtn = document.getElementById("check-btn");
const clearBtn = document.getElementById("clear-btn");
const output = document.getElementById("results-div");
const regex = /^1? ?(\(\d{3}\)|\d{3})[ -]?\d{3}[ -]?\d{4}$/;
const outputArr = [];
const checkInput = (str) => {
if (userInput.value === "") {
alert("Please provide a phone number");
} else if (regex.test(str)) {
outputArr.push(`Valid US number: ${str}`);
updateOutput(outputArr);
} else {
outputArr.push(`Invalid US number: ${str}`);
updateOutput(outputArr);
}
};
const updateOutput = (array) => {
output.innerHTML = "";
array.forEach((el) => {
output.innerHTML += `<h2 class="output-text">${el}</h2>`;
});
};
const clearOutput = () => {
outputArr.length = 0;
output.innerHTML = "";
};
checkBtn.addEventListener("click", () => {
checkInput(userInput.value);
userInput.value = "";
});
userInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
checkInput(userInput.value);
userInput.value = '';
}
});
clearBtn.addEventListener("click", clearOutput);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
Challenge Information:
Build a Telephone Number Validator Project - Build a Telephone Number Validator