So I wanted to write a if else statement where if Array.isArray is false, display a message. The function I have below allows user to enter an address, click Find ZipCode and does it thing. The function works, it just that if I input an invalid address, it doesn't show the message I wanted, which is invalid zipcode
In my HTML, I have:
<label>
Address:
<input name="address" type="text" id="address"></label>
<input type="button" value="Find ZipCode" id="find">
<p id="output"></p>
and Javascript:
var hello = function(data) {
var result = ""; // empty string to hold the value
var address = $("#address").val();
if (! Array.isArray(data))
{
$("#output").html("invalid zipcode");
}
else {
for (var i = 0; i < data.length; i++)
{
var zipcode = data[i].zipcode;
result += address + " zipcode: " + zipcode;
}
$("#output").html(result);
}
};
$("#find").click(function(){
var address = $("#address").val();
$("#output").html("Loading...");
$.getJSON("zipcodelookup.php", "address="+address , hello);
});