Telephone Validator Help

Hi All,

Trying to do the Telephone Validator project.
Managed to get it all working, well to me it looks like its all working.
But the tests say its not putting the results in to the results div.

Really appreciate any help with this! Thank you

html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Telephone Validator</title>
  </head>

  <body>
    <h1>Telephone Validator</h1>

    <input id = "user-input" maxlength="20">
    <button id = "check-btn" onclick = "press()">Press</button>
    <button id = "clear-btn" onclick = "clearBtn()">Clear</button>

    <div id = "results-div"></div>



    <script src = "script.js"></script>

  </body>
<html>

javascript:

let userInput = document.getElementById("user-input")
let resultsDiv = document.getElementById("results-div")

function press() {
    let valid = false;
    let userInputValue = userInput.value;
    let userInputValueLength = userInputValue.length
    console.log(userInputValueLength)
    if (userInputValueLength === 0) {
        alert ("Please provide a phone number")
    } else {
        check()
    }

}

function clearBtn() {
    console.log("clear button pressed")
    userInput.value = ""
    resultsDiv.textContent = ""
    
}

function check() {
    let userInputValue = userInput.value;
    let userInputValueLength = userInputValue.length
    var regex1 = /^[1][\s][0-9]{3}[-][0-9]{3}[-][0-9]{4}$/;            // 1 555-555-5555
    var regex2 = /^[1][(][0-9]{3}[)][\s][0-9]{3}[-][0-9]{4}$/;      // 1 (555) 555-5555
    var regex3 = /^[1][(][0-9]{3}[)][0-9]{3}[-][0-9]{4}$/;      // 1(555)555-5555
    var regex4 = /^[1][0-9]{3}[\s][0-9]{3}[\s][0-9]{4}$/;         // 1 555 555 5555
    var regex5 = /^[0-9]{3}[0-9]{3}[0-9]{4}$/;            // 5555555555
    var regex6 = /^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$/;            // 555-555-5555
    var regex7 = /^[(][0-9]{3}[)][0-9]{3}[-][0-9]{4}$/;            // (555)555-5555

    if (userInputValue.match(regex1)) {
        valid = true;
    } else if (userInputValue.match(regex2)) {
        valid = true;
    } else if (userInputValue.match(regex3)) {
        valid = true;
    } else if (userInputValue.match(regex4)) {
        valid = true;
    } else if (userInputValue.match(regex5)) {
        valid = true;
    } else if (userInputValue.match(regex6)) {
        valid = true;
    } else if (userInputValue.match(regex7)) {
        valid = true;
    } else {
        valid = false;
    }

    if (valid) {
        console.log(valid)
        resultsDiv.innerHTML += "<br>" + "Valid US number: " + userInputValue 
    } else {
        console.log(valid)
        resultsDiv.innerHTML += "<br>" + "Invalid US number: " + userInputValue
    }
}

Ah nevermind!!

Figured it out, I didnt define “valid”
added

let valid = false;

Then also some of my regex’s where incorrect

Corrected script.js:

let userInput = document.getElementById("user-input")
let resultsDiv = document.getElementById("results-div")

function press() {
    let valid = false;
    let userInputValue = userInput.value;
    let userInputValueLength = userInputValue.length
    console.log(userInputValueLength)
    if (userInputValueLength === 0) {
        alert ("Please provide a phone number")
    } else {
        check()
    }

}

function clearBtn() {
    console.log("clear button pressed")
    resultsDiv.textContent = ""
    
}

function check() {
    let valid = false;
    let userInputValue = userInput.value;
    let userInputValueLength = userInputValue.length
    var regex1 = /^[1][\s][0-9]{3}[-][0-9]{3}[-][0-9]{4}$/;            // 1 555-555-5555
    var regex2 = /^[1][\s][(][0-9]{3}[)][\s][0-9]{3}[-][0-9]{4}$/;      // 1 (555) 555-5555
    var regex3 = /^[1][(][0-9]{3}[)][0-9]{3}[-][0-9]{4}$/;      // 1(555)555-5555
    var regex4 = /^[1][\s][0-9]{3}[\s][0-9]{3}[\s][0-9]{4}$/;         // 1 555 555 5555
    var regex5 = /^[0-9]{3}[0-9]{3}[0-9]{4}$/;            // 5555555555
    var regex6 = /^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$/;            // 555-555-5555
    var regex7 = /^[(][0-9]{3}[)][0-9]{3}[-][0-9]{4}$/;            // (555)555-5555

    if (userInputValue.match(regex1)) {
        valid = true;
    } else if (userInputValue.match(regex2)) {
        valid = true;
    } else if (userInputValue.match(regex3)) {
        valid = true;
    } else if (userInputValue.match(regex4)) {
        valid = true;
    } else if (userInputValue.match(regex5)) {
        valid = true;
    } else if (userInputValue.match(regex6)) {
        valid = true;
    } else if (userInputValue.match(regex7)) {
        valid = true;
    } else {
        valid = false;
    }

    if (valid) {
        console.log(valid)
        resultsDiv.innerHTML += "<br>" + "Valid US number: " + userInputValue 
    } else {
        console.log(valid)
        resultsDiv.innerHTML += "<br>" + "Invalid US number: " + userInputValue
    }
}