0

In my signup form I have a field which requires that the user types in his/her address and city (autocomplete).

Problem is that it's also possible for the user to just type his/her city without the street and submit the form.

Does anyone has a solution for this?

<script>
  var input = document.getElementById('adres');
  var autocomplete = new google.maps.places.Autocomplete(input,{componentRestrictions: {country:'be'}});
  google.maps.event.addListener(autocomplete, 'place_changed', function(){
  var place = autocomplete.getPlace();
  })
</script>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
crievino
  • 63
  • 1
  • 8

2 Answers2

2

You can use autocomplete.setTypes(['address']); to allow only addresses in the autocomplete.

A city is not an address (it's a region), so the autocomplete will not allow it.

var input = document.getElementById('adres');
var autocomplete = new google.maps.places.Autocomplete(input,{componentRestrictions: {country:'be'}});
autocomplete.setTypes(['address']);
google.maps.event.addListener(autocomplete, 'place_changed', function(){
    var place = autocomplete.getPlace();
})
Dekel
  • 60,707
  • 10
  • 101
  • 129
  • Mhh, this doesn't work. It still returns Cities without a street as a result. Any user could select just the city and sign in but it would be a nice solution for this problem. Thx – crievino Jan 02 '17 at 23:33
  • what happen if you try `setTypes(['(regions)'])`? Are you able to write street names? (Basically you shouldn't, so if you do - there is some problem there...) – Dekel Jan 02 '17 at 23:36
  • I saw you replaced `autocomplete.setTypes(['address']);` from the last line to the 3th line and now it works! Thanks man, genius! Really appreciated. – crievino Jan 02 '17 at 23:58
  • Actually it shouldn't affect anything at all – Dekel Jan 02 '17 at 23:59
  • If I replace it to the last line, it does affect it ... No idea why. – crievino Jan 03 '17 at 00:06
0

I'm curious, you ask the person to type the complete address or you separate in various fields? Like one for street, another for state, city, etc, or is it just one field?

If you ask for address and people keep typing just the city for example, divide it in two inputs, one for the address name and another for the number, you could ask for the zip code. The important part is to make them know they need to add their address properly.

Also you could make validations, in the fields you could put "Address must have a '#'" to make sure there's a house number (Idk, that last one was an example)

  • Just one field. Indeed, making it two fields would solve the problem but I wanted to know if there was an easier fix for this. Thx – crievino Jan 02 '17 at 23:12