samedi 26 août 2017

What step am I missing to check if value matches array element and print an alert using if/else

I'm trying to check user input against predetermined list of zipcodes. I've created one variable that represents the user input using document.getElementById("zipcode").value and set up my array of zipcodes to check against. This worked once but I think I had break; included in the if/else. What am I missing? The input box translates to a string so I made my array elements string too. I'm so confused.

<h2>Zipcode checker</h2>

<input id="zipcode" name="address-4" type=text maxlength="5" value="" pattern="[0-9]" required/>`
<button id="btn" value="submit" type="submit" onclick="myStuff()">Register</button>

Script:

var btnInput = document.getElementById("zipcode").value;
var acceptedZip = ["85392", "85340", "85393", "85353", "85341"];
function myStuff() {
    for (var i = 0; i < acceptedZip.length; i++) {
        if (acceptedZip[i] === btnInput) {
            alert("we got you boo");
         } 
         else {
            alert("sorry son");
        }
    }
}   

Aucun commentaire:

Enregistrer un commentaire