Tell us what’s happening:
Heyyy, I have been trying this for some time now… I don’t understand where exactly I have gone wrong, the results are not showing…
Also, I believe that the clear button is working but for some reason the test keeps on failing.
It would be really appreciated if anyone could look into my code and check what I did wrong..
Thank youu
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head></head>
<body>
<h2>Telephone Number Validator</h2>
<input id="user-input">
<button id="check-btn" required>Check</button>
<button id="clear-btn" required>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-input");
const checkBtn = document.getElementById("check-btn");
const clearBtn = document.getElementById("clear-btn");
const result = document.getElementById("results-div");
checkBtn.addEventListener("click", (str) => {
if(input.value === "") {
window.alert("Please provide a phone number");
} else {
return validNumber;
}
});
const phoneNumber = (str) => {
let regex = /^(1\s?)?(\d{3}|\(\d{3}\))[\-\s]?\d{3}[\-\s]?\d{4}$/;
return regex.test(str);
}
console.log(phoneNumber("1(555)555-5555"));
const validNumber = () => {
if(phoneNumber = true) {
result.textContent = `Valid US number: ${input}`;
} else {
result = `Invalid US number: ${input}`;
}
};
const clear = () => {
input.value = "";
result.remove();
};
clearBtn.addEventListener("click", clear);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Challenge Information:
Build a Telephone Number Validator Project - Build a Telephone Number Validator