I’m attempting to validate my location input field against google’s map geocoder API. IF the address is valid save it to a hidden field if it’s invalid empty the hidden field. However, Geocoder is always responding back with ‘OK’ regardless of the input value. I’m not sure why or how to get this to work.
my code:
[code=html]<input class=”form-control” type=”text” id=”joblocation” placeholder=”Enter a Location (ie. Phoenix, AZ)” value=”<?php echo set_value(‘location’); ?>” name=”location” onchange=”doGeocode()” />
[CODE]
<script>
var input = document.getElementById(‘joblocation’);
var options = {
types: [‘(cities)’]
};
autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.setComponentRestrictions({‘country’: [‘us’, ‘ca’, ‘au’, ‘ge’, ‘in’, ‘uy’]});
function doGeocode(){
var addr = document.getElementById(“joblocation”);
// Get geocoder instance
var geocoder = new google.maps.Geocoder();
// Geocode the address
geocoder.geocode({‘address’: addr.value}, function(results, status){
if (status === google.maps.GeocoderStatus.OK && results.length > 0) {
alert(“Valid Address”);
//do something
// show an error if it’s not
} else {
alert(“Invalid address”);
}
});
};
</script>