Back

Example: Lookup user's country

Set defaultCountry to 'auto' and pass in a function for geoIpLookup to perform a JSONP request to ipinfo.io, which returns the user's country based on their IP address.

Markup

<input id="phone" type="tel">

Code

$("#phone").intlTelInput({
  defaultCountry: "auto",
  geoIpLookup: function(callback) {
    $.get('http://ipinfo.io', function() {}, "jsonp").always(function(resp) {
      var countryCode = (resp && resp.country) ? resp.country : "";
      callback(countryCode);
    });
  },
  utilsScript: "../../lib/libphonenumber/build/utils.js" // just for formatting/placeholders etc
});

Result