intl-tel-input
intl-tel-input copied to clipboard
setNumber/getNumber
Steps to reproduce
- create phone input object
- call setNumber(valid number)
- call getNumber()
Expected behaviour
getNumber should return valid number provided in the setNumber call
Actual behaviour
getNumber return empty string if calling setTimeout(getNumber,2000) the right number will be returned, so empty string is only returned if calling getNumber immediatly after calling setNumber
Initialisation options
autoPlaceholder: "aggressive",
initialCountry: "auto",
geoIpLookup: function(success) {
// Get your api-key at https://ipdata.co/
fetch("https://api.ipdata.co/?api-key=mykey")
.then(function(response) {
if (!response.ok) return success("");
return response.json();
})
.then(function(ipdata) {
success(ipdata.country_code);
});
},
separateDialCode: true,
preferredCountries: ["us", "dk", "se", "de", "tw"],
utilsScript:
"https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js",
If you can find the minimal set of code that reproduces the problem and put that in a CodePen, then I'd be happy to take a look. Here is an example pen with the plugin up and running - feel free to fork this if it helps.
I'm having the same issue, still not fixed.
function setDefaultNumber() { let preloadedNumber = '308113853'; phoneInput.setNumber(preloadedNumber); console.log(phoneInput.getNumber()); }
With this, I got nothing in the console.
@boldizsarf I was having issues with this, it may not help but my issue was actually because I was using an outdated jQuery with the version of int-tel-input. I'd make sure that you're using the latest version of both int-tel-input and jQuery if not doing so already
I'm using the latest jQuery and the latest version of int-tel-input, and still not working.
@jackocnr here's the pen that reproduces the bug https://codepen.io/fetis/pen/oNqNVQK?editors=1111
if you call it immediately -> empty string if you click button to get a value -> value is returned but not in international format
It's seems the issue comes from the "window.inTelInputUtils" which is not yet loaded when the first call of getNumber() is called due to the fact the documentReady() function return false because the document.readState is "loading" and not yet complete. So, while the window.AddEventListener which listen the "load" event is not trigger, and which seems coming after the call of getNumber(), issue seems there. This listener is defined in the code of the "_initRequests" function.
Had the same issue. In my case, it was because my intlTelInput was not initialized quick enough before I attempted to get the number. Initializing it early before I attempted to get the phone number worked.
As others have pointed out, this is a race condition. getNumber
only works when the utils script has been loaded, which is an async process, so you need to make sure that has finished before you call it. If you check the docs for utilsScript
option, you will see info about how the initialisation returns a promise that you can use to know when it's finished loading.