I am a noob when it comes to JavaScript, but I have been having an issue trying to figure this out. Any help will be appreciated. Samples, and tutorial sites will help out too. My question is how would I incorporate this search function to search a JSON file with zip codes and return the array with that zip and the other data that goes along with it. Sorry if this doesn't seem like any research was done, believe me I've been playing with this too long and I just need some direction to get me started. Thanks in advance.
So I have this form:
<form>
<input type="text" name="zip" id="zip" placeholder="Zip Code">
<button onclick="getZip(event);">Check</button>
</form>
JavaScript:
<script>
function getZip(e){
e.preventDefault();
var zip = document.getElementById('zip').value; // Gets the value from the text field "zip" on the form.
if (zip === "") { // Checks if the text field "zip" is empty, if so it returns an error message.
alert ('Please enter a zip code!');
}
else if (zip.length > 5) {
alert ("Please enter a valid 5 digit numeric zip code.");
}
else if (isNaN(zip)) { // Checks if the text field "zip" for alphabetic characters. The "isNaN" function is to determine if a value does not convert to a number, if so it returns an error message.
alert("Please enter a numeric value.");
} else if (zip.length == 5) {
alert("Success! " + zip);
} else { // Returns value from the text field "zip" on the form.
alert(zip);
}
return false;
}
</script>
JSON Example:
[ {
"ZipCode": 48650,
"City": "Pinconning",
"State": "Michigan",
"StateAbbreviation": "MI",
"County": "Bay"
},
{
"ZipCode": 48651,
"City": "Prudenville",
"State": "Michigan",
"StateAbbreviation": "MI",
"County": "Roscommon"
}
]