jackocnr / intl-tel-input

A JavaScript plugin for entering and validating international telephone numbers
https://intl-tel-input.com
MIT License
7.36k stars 1.92k forks source link

Default global icon for invalid dial code #1646

Closed minhluan259 closed 1 month ago

minhluan259 commented 1 month ago

Plugin version

e.g. v23.0.10 (please try latest version)

Steps to reproduce

  1. Set default telephone number 12345
  2. Initial intl-tel-input on page load

Expected behaviour

Display a global icon if there is a default phone number already set with an invalid dial code. Screenshot_10

Actual behaviour

Only arrow dropdown showing

jackocnr commented 1 month ago

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.

jackocnr commented 1 month ago

It seems to work fine here: https://codepen.io/jackocnr/pen/NWVgMRp?editors=1010

So I'm not sure what the problem is. Can you reproduce it in a codepen?

minhluan259 commented 1 month ago

The problem occurs when I use https://ipinfo.io/json

const input = document.querySelector("#phone");

const iti = window.intlTelInput(input, {
  utilsScript:
    "https://cdn.jsdelivr.net/npm/intl-tel-input@23.0.10/build/js/utils.js",
  initialCountry: "auto",
  nationalMode: 1,
  strictMode: 1,
  separateDialCode: 1,
  autoPlaceholder: "aggressive",
  geoIpLookup: (callback) => {
    fetch("https://ipinfo.io/json")
      .then(res => res.json())
      .then(data => callback(data.country.toLowerCase()))
      .catch(() => callback("us"));
  }
});

// store the instance variable so we can access it in the console e.g. window.iti.getNumber()
window.iti = iti;
jackocnr commented 1 month ago

Yes well it doesn't make sense to initialise the plugin with a phone number in the input, and then to set initialCountry=auto. If you already have a number, then you must already know the country, so either you (A) include the international dial code in the initial input value, which will automatically update the selected country (recommended), or (B) use initialCountry to set the correct country for the given number.

minhluan259 commented 1 month ago

Initialize the plugin with a phone number in the input field in cases where we have a list of registered users with phone numbers but without country dial codes. Users will need to manually update the dial code after we integrate this plugin. Therefore, only the arrow showing may result in poor user experience.

jackocnr commented 1 month ago

Ok I'll take a look when I have a moment, and try to get it so it always shows either a flag or the globe icon no matter what initialisation options are used.

jackocnr commented 1 month ago

Fix released in v23.0.11 - give it a try and let me know if it works for you!

minhluan259 commented 4 weeks ago

Confirmed, it works. Thank you.