Automatic identification of phone number mask by ip
Good afternoon!
As you know, in the phone element we have added the possibility of selecting entering a phone number witha country flag:
We were approached by users with a request - auto-detection of the country by ip.
To add such functionality, you need to add a script custom code:
Select "Custom code" in the form settings and click "Open Script Manager":
Click "Add Code":
Specify a name for the new code, select the "Script coonnection point" - body...</body>, in the "Type of connection" list - On a form page:
In the code field, you need to specify the following script:
<script> function geoIpLookup(callback, countries) { $.get('https://ipinfo.io', function() {}, "jsonp").always(function(resp) { var countryCode = (resp && resp.country) ? resp.country : "us"; if (!countries.length || countries.indexOf(countryCode) !== -1) { callback(countryCode); } else { callback(countries[0] || countryCode); } }); } </script>
Where the parameter resp.country: "us", by default, the country is displayed if there is no country in the list of available countries for the phone number:
Accordingly, instead of the value us (USA), you can specify a different value.
After you have added the script to the "Code" field, do not forget to click the "Save" button. The added code will be sent to the administrators for moderation:
After the code is approved, the approved status will appear and then it can be connected to the form. Open the list of scripts that can be added to the form:
Select the approved script and click "Add to Form":
The code will be displayed in the "List of used scripts in the form", click "Save":
Now, after opening the form, the value of the country code defined by the ip address will be set in the phone field: