Created
October 21, 2023 23:50
-
-
Save akserikawa/a898af26366818d399c896faebaee0eb to your computer and use it in GitHub Desktop.
Dropdown phonepicker with intl support + IP geolocalization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/css/intlTelInput.css" /> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/js/intlTelInput.min.js"></script> | |
<script> | |
const phoneInputField = document.querySelector("#phone"); | |
const form = document.querySelector("#form"); | |
const errorMessage = document.querySelector("#client-phone-validation"); | |
const phoneInput = window.intlTelInput(phoneInputField, { | |
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.19/js/utils.js", | |
initialCountry: "auto", | |
hiddenInput: "phoneNumber", | |
geoIpLookup: callback => { | |
fetch("https://ipapi.co/json") | |
.then(res => res.json()) | |
.then(data => callback(data.country_code)) | |
.catch(() => callback("us")); | |
}, | |
preferredCountries: ['es'] | |
}); | |
form.onsubmit = () => { | |
if (!phoneInput.isValidNumber()) { | |
errorMessage.classList.remove('hidden'); | |
return false; | |
} | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment