Tell us what’s happening:
I’m working on validating US phone numbers in my script, but two test cases aren’t passing:
When a valid number is entered, the result should be “Valid US number: [number]”.
For an invalid number, the result should be “Invalid US number: [number]”.
Currently, the output is not displaying correctly for these cases. Can anyone help me identify what’s wrong with my code or the validation logic? Thanks!
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">
<title>Phone Number Validation</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<input type="text" id="user-input" placeholder="Enter phone number">
<button id="check-btn">Check Number</button>
<button id="clear-btn">Clear</button>
<div id="results-div"></div>
</div>
<script src="script.js"></script>
</body>
</html>
/* file: script.js */
document.getElementById("check-btn").addEventListener("click", function () {
const input = document.getElementById("user-input").value.trim();
const resultDiv = document.getElementById("results-div");
if (!input) {
alert("Please provide a phone number");
return;
}
const isValid = validatePhoneNumber(input);
if (isValid) {
resultDiv.textContent = `Valid US number: ${input}`;
resultDiv.style.color = "green";
} else {
resultDiv.textContent = `Invalid US number: ${input}`;
resultDiv.style.color = "red";
}
});
document.getElementById("clear-btn").addEventListener("click", function () {
document.getElementById("results-div").textContent = "";
document.getElementById("user-input").value = "";
});
function validatePhoneNumber(number) {
const regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s-]?\d{3}[\s-]?\d{4}$/
return regex.test(number);
}
/* file: styles.css */
/* General styles */
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
width: 300px;
text-align: center;
}
/* Styling the input field */
input[type="text"] {
width: 100%;
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 4px;
margin-bottom: 15px;
box-sizing: border-box;
}
/* Button styles */
button {
width: 45%;
padding: 10px;
font-size: 16px;
color: #fff;
background-color: #4CAF50;
border: none;
border-radius: 4px;
cursor: pointer;
margin: 5px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #45a049;
}
/* Results display */
#results-div {
font-size: 16px;
font-weight: bold;
margin-top: 20px;
}
/* Green color for valid */
#results-div.valid {
color: green;
}
/* Red color for invalid */
#results-div.invalid {
color: red;
}
/* Clear button style */
#clear-btn {
background-color: #f44336;
}
#clear-btn:hover {
background-color: #e53935;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Challenge Information:
Build a Telephone Number Validator Project - Build a Telephone Number Validator