intl-tel-input icon indicating copy to clipboard operation
intl-tel-input copied to clipboard

initialCountry should gracefully use a default if given bad data

Open mattsly opened this issue 4 years ago • 2 comments

Currently there is a leap of faith that the value used for initialCountry will succeed in a lookup...if for whatever reason it doesn't (which seems like a possibility since that value is coming from a 3rd party service like ipinfo or perhaps from existing data in the application) then initialization will fail.

I suggest taking a more defensive approach to handle this possibility and default to the preferred country.

Steps to reproduce

  1. Initialize with an unsupported value in the initialCountry parameter
window.intlTelInput(input, {
    utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.13/js/utils.min.js",
    initialCountry: 'baddata',
    preferredCountries: ["us"],
  });

Expected behaviour

A defensive approach that catches this condition and uses the default / preferred country value, but still initializes the UI

Actual behaviour

JavaScript exception and the form element is unusable

image

mattsly avatar Aug 17 '21 13:08 mattsly

Yeah that would make sense, thanks for raising. I'd be open to a PR.

jackocnr avatar Oct 23 '21 19:10 jackocnr

Is there a way to have intlTelInput country flag set to empty and force the user to select their country code ?

tech-thanoshotels avatar Apr 11 '22 09:04 tech-thanoshotels

@tech-thanoshotels no this is not currently possible, and would be too much work to implement - I'm not interested in adding this functionality for the time being.

jackocnr avatar Jul 14 '23 16:07 jackocnr

There's a fix for this in master, which will be included in the next release (soon).

jackocnr avatar Jan 14 '24 16:01 jackocnr