Rename Zip and State labels based on users location (Country) using IP address

This is for fellow knacksters who have users from different countries and use the address field. This code takes the users IP address and determines what country they are in to change the terminology address labels for Zip and State.

If for example I’m in the UK ‘Zip’ changes to ‘Postcode’ and ‘State’ = ‘County’. If then via your IP address you are in another country then we can change the labels based on that country.

Apologies I have only added If in the UK but have added If else for you to use other destinations to the code.

$(document).on('knack-page-render.any', function() {
$.getJSON("https://ipapi.co/json/", function(data) {
  var country = data.country_name;
console.log(country)
  if (country === "United Kingdom") {
    $('label.help[for="state"]').text('County')
    $('label.help[for="zip"]').text('Post Code')
  } else if (country === "United States") {
    $('label.help[for="state"]').text('State')
    $('label.help[for="zip"]').text('Zip')
// Copy this for as many locations you want to add and change the name of the location
  } else if (country === "Netherlands") {
       $('label.help[for="zip"]').text('Post Code')
//
  } else {
    // do something here if Your location could not be determined
  }
});
});

If anyone needs help adding additional locations or you have any further ideas based on location then please add your comments below.

3 Likes

Another useful bolt on Johnny - thank you.

1 Like

Hey, I hope you’ve been enjoying these code snippets. I wanted to let you know that all of my code is now available on my new platform, Appknowledged. Appknowledged has taken over from KnackMods. I encourage all of you to sign up—it’s completely free—and you’ll have access to all of my free content.

1 Like