Tell us what’s happening:
My code is functioning and returning the correct invalid/valid phone numbers on CodePen. I know there was a thread about there being a bug for this cert, is there something I can change so my project passes?
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport"
content="width=device-width, intial-scale=1" />
<link rel="stylesheet" href="./style.css" />
<title>Telephone Number Validator</title>
</head>
<input id="user-input"></input>
<button id="check-btn">CHECK</button>
<button id="clear-btn">CLEAR</button>
<div id="results-div"></div>
/* file: styles.css */
/* file: script.js */
const userInput = document.getElementById("user-input");
const checkBtn = document.getElementById("check-btn");
const clearBtn = document.getElementById("clear-btn");
const resultsDiv = document.getElementById("results-div");
const phoneRegex = /\W+|[a-z]/gi;
const inputArray = userInput.value.split();
const checkInput = () => {
if (userInput.value === "") {
alert("Please provide a phone number");
return;
} else {
isValidNumber();
}
};
const isValidNumber = () => {
const cleanNumber = userInput.value.replace(phoneRegex, "");
const inputArray = cleanNumber.split("");
if (inputArray[0] === "1") {
if (inputArray.length === 10 || inputArray.length === 11) {
resultsDiv.textContent = `Valid US number: ${userInput.value}`;
} else { resultsDiv.textContent = `Invalid US number: ${userInput.value}`;}
}
else if (inputArray.length === 9 || inputArray.length === 10) {
resultsDiv.textContent = `Valid US number: ${userInput.value}`;
} else { resultsDiv.textContent = `Invalid US number: ${userInput.value}`;}
};
const clearResults = () => {
resultsDiv.textContent = "";
};
checkBtn.addEventListener("click", checkInput);
clearBtn.addEventListener("click", clearResults);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Challenge Information:
Build a Telephone Number Validator Project - Build a Telephone Number Validator