Tell us what’s happening:
My code is working, but the test is still failing. Am I just doing it in a way that FCC doesn’t want me to?
I’m getting these errors for every test:
-
When the #user-input element contains a valid US number and the #check-btn element is clicked, the #results-div element should contain the text "Valid US number: " followed by the number.
-
When the #user-input element contains an invalid US number and the #check-btn element is clicked, the #results-div element should contain the text "Invalid US number: " followed by the number.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="styles.css">
<meta charset="UTF-8">
<title>Phone Validation</title>
</head>
<body>
<header>
<div id="banner">
<h1 id="title">Phone Number Validator</h1>
</div>
</header>
<main>
<div id="input">
<input id="user-input" type="tel" placeholder="Enter your phone number:">
<button id="check-btn" type="button" onclick="checkNum()">Check</button>
<button id="clear-btn" type="button" onclick="clearNum()">Clear</button>
</div>
<div id="results-div">
</div>
</main>
<script language="JavaScript" type="text/javascript" src="script.js"></script>
</body>
</html>
/* file: script.js */
let phoneHistory = [];
function checkNum() {
const input = document.getElementById("user-input").value.trim();
const resultsDiv = document.getElementById("results-div");
const regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/;
if (input === "") {
alert("Please provide a phone number");
return;
}
if (regex.test(input)) {
phoneHistory.push(`<span style="color: green;">Valid US number: ${input}</span>`);
} else {
phoneHistory.push(`<span style="color: red;">Invalid US number: ${input}</span>`);
}
const reversedHistory = phoneHistory.slice().reverse();
document.getElementById("user-input").value = "";
resultsDiv.innerHTML = reversedHistory.join("<br>");
resultsDiv.style.display = "block";
resultsDiv.style.backgroundColor = "black";
resultsDiv.style.textAlign = "center";
}
function clearNum() {
document.getElementById("user-input").value = "";
phoneHistory = [];
const resultsDiv = document.getElementById("results-div");
resultsDiv.innerHTML = "";
resultsDiv.style.display = "none";
}
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 OPR/116.0.0.0
Challenge Information:
Build a Telephone Number Validator Project - Build a Telephone Number Validator