【问题标题】:How to check address validity? [duplicate]如何检查地址的有效性? [复制]
【发布时间】:2015-05-02 00:46:43
【问题描述】:

我写了下面的例子:

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);

现在它运行良好,但我需要检查当按钮单击时 uset 没有键入类似“dsgfdsgfjhfg”的内容。

请帮助改进我的代码。

附言

这大约是我想要的,但它在回调中执行。我需要一个返回 true 或 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;
            }
          });       
    }

【问题讨论】:

  • 您希望如何验证输入?我的意思是,好的地址与坏地址的规则是什么?
  • @Pointy 我想验证谷歌是否理解输入地址
  • @Pointy 请阅读更新
  • 只要您必须异步请求源,您就无法从函数中获得响应作为返回。
  • 在我看来,“Autocomplete()”API 不会告诉您地址是否无效;它所做的只是在用户选择地址时触发一个事件。无论如何,再一次,您不能从这样的异步回调系统返回值。这根本不可能。相反,您必须传入自己的回调来处理“isValid”标志。

标签: javascript google-maps google-maps-api-3 geocoding


【解决方案1】:

您将不得不重新设计地址检查器函数,以便传入回调函数。从异步操作中返回值本质上是没有意义的。你会想要这样的东西:

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);
            }
          });       
    }

调用这个函数:

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

【讨论】:

  • 我有以下监听器:$("#editTerminal200").submit(function(e) { return editTerminal(200,e); });
  • editTerminal 调用 codeEditAddress
  • @gstackoverflow 对。那是行不通的。您必须做到始终阻止默认表单提交,然后在验证地址有效时在回调处理程序中自行提交表单。
  • 我简化了我的代码,但它不起作用jsfiddle.net/SDPHm/689
  • 我不知道我要输入多少次:你不能从异步操作中返回值。它根本行不通。
猜你喜欢
  • 2019-05-19
  • 2021-09-01
  • 1970-01-01
  • 2016-11-29
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多