Build a Telephone Number Validator Project - Build a Telephone Number Validator

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

Hi and welcome to the forum :wave:

Do you see the ReferenceError in the console when you run the tests?

[ReferenceError: resultsDiv is not defined]
[ReferenceError: resultsDiv is not defined]

Yes, I’m seeing the ReferenceError: resultsDiv is not defined in the console when I run the tests.

// console output
[ReferenceError: resultsDiv is not defined]
[ReferenceError: resultsDiv is not defined]

You should not get an error like that, right?

Yes, I know i shouldn’t got that error. When I run it with Ctrl + Enter, I get this output:

// running tests
35. 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.
36. 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.
// tests completed
// console output
[ReferenceError: resultsDiv is not defined]
[ReferenceError: resultsDiv is not defined]

But when I manually test the project by entering a number, it works and even gives the valid or invalid message along with the number I input.

Hm, looks like a bug, try this:

https://forum.freecodecamp.org/t/build-a-telephone-number-validator-project-build-a-telephone-number-validator/735029/2?u=pkdvalis

There is bug in the tests. If you change the following variables and their occurrences in code, everything will pass:
inputuserInput
checkcheckBtn
resultresultsDiv

I made the suggested changes to the variable names (input to userInput, check to checkBtn, and result to resultsDiv), but the tests are still failing, particularly on variables 35 and 36. Could you please clarify what needs to be adjusted further to pass the tests?

document.getElementById("check-btn").addEventListener("click", function () {
    const userInput = document.getElementById("user-input").value.trim();
    const resultsDiv = document.getElementById("results-div");

    if (!userInput) {
        alert("Please provide a phone number");
        return;
    }

    const isValid = validatePhoneNumber(userInput);

    if (isValid) {
        resultsDiv.textContent = `Valid US number: ${userInput}`;
        resultsDiv.style.color = "green";
    } else {
        resultsDiv.textContent = `Invalid US number: ${userInput}`;
        resultsDiv.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);
}

Declare these in the global scope (outside of the functions/callback) and it should work:

const userInput = document.getElementById('user-input');
const checkBtn = document.getElementById('check-btn');
const resultsDiv = document.getElementById('results-div');
1 Like

Thanks a lot for your help! I’ve been stuck on this since last night, and your guidance finally got me through it. I really appreciate it! :sweat_smile:

Just to confirm, did this work?

Is this still considered a bug? We have other people stuck with this as well and I don’t see a github issue for it, I was in the middle of opening one.

Yes, It pass all of the test and worked

1 Like

There isn’t separate issue, but there’s PR with fix fix(curriculum): undeclared variables in tests by c0d1ng-ma5ter · Pull Request #58692 · freeCodeCamp/freeCodeCamp · GitHub

1 Like