-1

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;
            }
          });       
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • 1
    How exactly do you want to validate the input? I mean, what are the rules for a good address versus a bad address? – Pointy Mar 01 '15 at 17:35
  • @Pointy I want to validate that google understand input adsress – gstackoverflow Mar 01 '15 at 17:37
  • @Pointy please read update – gstackoverflow Mar 01 '15 at 17:41
  • 1
    As long as you have to request a source asynchronously you can't get the response as return from the function. – Dr.Molle Mar 01 '15 at 17:46
  • 1
    It doesn't look to me like that "Autocomplete()" API tells you whether addresses are invalid; all it does is fire an event when the user has chosen an address. In any case, once again, you cannot return a value from an asynchronous callback system like that. It is simply not possible. Instead, you have to pass in your own callback to handle the "isValid" flag. – Pointy Mar 01 '15 at 17:47
  • following function executes when I submit form. If adress is wrong I should not submit form i.e. I should to return FALSE at this case – gstackoverflow Mar 01 '15 at 17:51

1 Answers1

0

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
  }
});
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • I have following listener: **$("#editTerminal200").submit(function(e) { return editTerminal(200,e); });** – gstackoverflow Mar 01 '15 at 19:03
  • editTerminal invokes codeEditAddress – gstackoverflow Mar 01 '15 at 19:04
  • @gstackoverflow right. That won't work. You'll have to make it so that you *always* prevent the default form submit, and then submit the form yourself in the callback handler when you verify that the address was valid. – Pointy Mar 01 '15 at 19:17
  • I simplified my code and it doesn't work http://jsfiddle.net/SDPHm/689/ – gstackoverflow Mar 01 '15 at 20:53
  • I don't know how many times I am going to have to type this: **you cannot return values from an asynchronous operation.** It simply will not work. – Pointy Mar 02 '15 at 01:10
  • I have understood you. But it is very bad that I cannot create utility method which returns true(if address is valid) or false (if adsress invalid) – gstackoverflow Mar 02 '15 at 09:16