如何检查地址有效性? [英] How to check address validity?
问题描述
我写了下面的例子:
http: code>< label for =searchTextField>请插入地址:< / label> js: 现在它运行良好,但我需要检查uset没有输入类似dsgfdsgfjhfg当按钮点击。 请帮助改进我的代码。 这大概是我想要的,但它在回调中执行。我需要一个返回true或false的函数。 您将会拥有重新设计您的地址检查器功能,以便传递回调函数。从异步操作中返回一个值本质上是没有意义的。你会想要这样的东西: 调用这个函数: I have wrote following example: http://jsfiddle.net/214190tj/1/ html: js: 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. this approximately make what I want but it executes in callback. I need a function which returns true or 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: To call this function:
这篇关于如何检查地址有效性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b
< br>
< input id =searchTextFieldtype =textsize =50>
< input type =submitvalue =is valid>
var input = document.getElementById('searchTextField');
var options = {componentRestrictions:{country:'us'}};
new google.maps.places.Autocomplete(input,options);
PS
函数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 {
//显示错误信息等
}
});
<label for="searchTextField">Please Insert an address:</label>
<br>
<input id="searchTextField" type="text" size="50">
<input type="submit" value="is valid">
var input = document.getElementById('searchTextField');
var options = {componentRestrictions: {country: 'us'}};
new google.maps.places.Autocomplete(input, options);
P.S.
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;
}
});
}
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
}
});