I'm using the "mask" feature (from plugin jquery mask ) and the plugin intlTelInput.
The problem is that when changing the country selection, the Placeholder is incorrect. Example: if the selection is in the country Brazil, with the placeholder (11) 96123-4567, and when switching to any other country like Botswana and returning the selection to Brazil again, the placeholder is (11) 96123, which is incorrect.
My code is below:
// Make sure to place this snippet in the footer or at least after
// the HTML input we're targeting.
$(document).ready(function() {
var phoneInputID = "#phone";
var input = document.querySelector(phoneInputID);
var iti = window.intlTelInput(input, {
// allowDropdown: false,
// autoHideDialCode: false,
// autoPlaceholder: "off",
// dropdownContainer: document.body,
// excludeCountries: ["us"],
formatOnDisplay: true,
initialCountry: "BR",
// geoIpLookup: function(callback) {
// $.get("http://ipinfo.io", function() {}, "jsonp").always(function(resp) {
// var countryCode = (resp && resp.country) ? resp.country : "";
// callback(countryCode);
// });
// },
hiddenInput: "full_number",
// initialCountry: "auto",
// localizedCountries: { 'de': 'Deutschland' },
// nationalMode: false,
// onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
// placeholderNumberType: "MOBILE",
preferredCountries: ['es'],
// separateDialCode: true,
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/11.0.14/js/utils.js"
});
$(phoneInputID).on("countrychange", function(event) {
// Get the selected country data to know which country is selected.
var selectedCountryData = iti.getSelectedCountryData();
// Get an example number for the selected country to use as placeholder.
newPlaceholder = intlTelInputUtils.getExampleNumber(selectedCountryData.iso2, true, intlTelInputUtils.numberFormat.INTERNATIONAL),
// Reset the phone number input.
iti.setNumber("");
// Convert placeholder as exploitable mask by replacing all 1-9 numbers with 0s
mask = newPlaceholder.replace(/[1-9]/g, "0");
// Apply the new mask for the input
$(this).mask(mask);
});
// When the plugin loads for the first time, we have to trigger the "countrychange" event manually,
// but after making sure that the plugin is fully loaded by associating handler to the promise of the
// plugin instance.
iti.promise.then(function() {
$(phoneInputID).trigger("countrychange");
});
});
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.12/js/intlTelInput.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.11/jquery.mask.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.12/css/intlTelInput.css" rel="stylesheet" />
</head>
<body>
<input type="tel" id="phone" name="phone" />
</body>
</html>
The solution would be for the Placeholder to be correct when changing selection.