Thursday 1 February 2018

Autofill state and city based on zipcode

https://nobleintentstudio.com/blog/zip-code-to-city-state-lookup/


$('#input').blur(function(){

  var zip = $(this).val();
  var city = '';
  var state = '';

  //make a request to the google geocode api
  $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address='+zip+'',function(response){
    //find the city and state
    var address_components = response.results[0].address_components;
    $.each(address_components, function(index, component){
      var types = component.types;
      $.each(types, function(index, type){
        if(type == 'locality') {
          city = component.long_name;
        }
        if(type == 'administrative_area_level_1') {
          state = component.long_name;
        }
       
        if(type == 'country'){
          country = component.long_name;
        }
      });
    });

    //pre-fill the city and state
    $('#City').val(city);
    $('#State').val(state);
    $('#Country').val(country);
  });
});

No comments:

Post a Comment

Featured post

Data connections in Infopath forms

https://www.qdoscc.com/blog/how-automatically-retrieve-current-username-infopath-sharepoint-list-form

Popular Posts