How to check address validity? [duplicate]

烂漫一生 提交于 2019-12-12 06:56:48

问题


I have wrote following example:

http://jsfiddle.net/214190tj/1/

html:

<label for="searchTextField">Please Insert an address:</label>
<br>
<input id="searchTextField" type="text" size="50">
<input type="submit" value="is valid">

js:

var input = document.getElementById('searchTextField');
var options = {componentRestrictions: {country: 'us'}};

new google.maps.places.Autocomplete(input, options);

Now it is working good but I need to check that uset didn't type something like "dsgfdsgfjhfg" when button clicks.

Please help to improve my code.

P.S.

this approximately make what I want but it executes in callback. I need a function which returns true or false.

function codeEditAddress(id) {
        var address = document.getElementById('address' + id).value;
        isValid = undefined;
        geocoder.geocode({ 'address': address}, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                $("#mapLat" + id).val(results[0].geometry.location.lat());
                $("#mapLng" + id).val(results[0].geometry.location.lng());
                if (marker) {
                    marker.setMap(null);
                }
                marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
                marker.setMap(map);
                isValid = true;
            } else {               
                isValid = false;
            }
          });       
    }

回答1:


You're going to have to re-design your address checker function so that you pass in a callback function. Returning a value from an asynchronous operation inherently does not make sense. You'll want something like this:

function codeEditAddress(id, callback) {
        var address = document.getElementById('address' + id).value;
        geocoder.geocode({ 'address': address}, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                $("#mapLat" + id).val(results[0].geometry.location.lat());
                $("#mapLng" + id).val(results[0].geometry.location.lng());
                if (marker) {
                    marker.setMap(null);
                }
                marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
                marker.setMap(map);
                callback(true);
            } else {               
                callback(false);
            }
          });       
    }

To call this function:

codeEditAddress(id, function(isValid) {
  if (isValid) {
    // submit form, do whatever
  }
  else {
    // show error message, etc
  }
});


来源:https://stackoverflow.com/questions/28796705/how-to-check-address-validity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!