member tips

Update the country selector of the phone field based on user location

  • 8 February 2023
  • 2 replies
  • 250 views
Update the country selector of the phone field based on user location
Userlevel 5
Badge +5

The phone field allows to select a default option for the country. Is there a way to detect dynamically which country the user is in, using a service like ipinfo.io, and update the select country, for the user not to have to select it before entering the local part of the phone number?


2 replies

Userlevel 5
Badge +5

I found a way to do it using Javascript (TF Business plan or above only). See below some code ideas. Yet, I think it might be an interesting idea to be able to pass a default_country_code hidden field, so that we can dynamically populate that field using the embed API, and the form automatically update the flags when loading, if detected.


If the field is a phone number

window.rendererData.form.fields[5].properties.default_country_code='ES'

If the field is a phone number inside a Contact Card

window.rendererData.form.fields[4].properties.fields[2].properties.default_country_code = 'FR'

Note, you need to modify the number inside the brackets [] to match the position of the field in your form. Or you can loop through all the fields to detect the type "phone_number"

Userlevel 7
Badge +5

Very cool solution, thanks a lot for figuring this one out @jeremielp 😎 Featuring your post on the front page of the Community as I reckon this is something others might find useful!

Reply