如何检查地址有效性? [英] How to check address validity?

查看:90
本文介绍了如何检查地址有效性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的例子:

http:
$ b

code>< label for =searchTextField>请插入地址:< / label>
< br>
< input id =searchTextFieldtype =textsize =50>
< input type =submitvalue =is valid>

js:

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

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

现在它运行良好,但我需要检查uset没有输入类似dsgfdsgfjhfg当按钮点击。



请帮助改进我的代码。



PS



这大概是我想要的,但它在回调中执行。我需要一个返回true或false的函数。

 函数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+ (标记){
marker.setMap(null);
}
marker =结果[0] .geometry.location.lng());新的google.maps.Marker({
map:map,
position:results [0] .geometry.location
});
marker.setMap(map);
isValid = true;
} else {
isValid = false;
}
});


解决方案

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

$ $ p $ 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+ (标记){
marker.setMap(null);
}
marker =结果[0] .geometry.location.lng());新的google.maps.Marker({
map:map,
position:results [0] .geometry.location
});
marker.setMap(map);
callback(true);
} else {
callback(false);
}
});
}

调用这个函数:

  codeEditAddress(id,function(isValid){
if(isValid){
//提交表单,做任何
}
else {
//显示错误信息等
}
});


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

解决方案

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

这篇关于如何检查地址有效性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆