jackocnr / intl-tel-input

A JavaScript plugin for entering and validating international telephone numbers. Now includes React and Vue components.
https://intl-tel-input.com
MIT License
7.57k stars 1.94k forks source link

Why when i use geoIpLookup and set the initialCountry: "auto" but the result on webpage is undefined? #955

Closed fransiscushermanto closed 5 years ago

fransiscushermanto commented 5 years ago

Steps to reproduce

  1. Set `window.intlTelInput(input, {

        preferredCountries: ["id", "sg"],
        initialCountry: "auto",
        geoIpLookup: function(callback) {
            $.get('https://ipinfo.io', function() {}, "jsonp").always(function(resp) {
                var countryCode = (resp && resp.country) ? resp.country : "";
                callback(countryCode);
            });
        },
    
        utilsScript: "<?php echo BASE_URL . "asset/intl-tel-input-master/build/js/utils.js?1562189064761"; ?>"
    });`

Expected behaviour

As my Ip address is from Indonesia it should pick Indonesia

Actual behaviour

It is undefined, but the callback success pass the information and it show all the information about city, country, ip, etc.

here is the picture of the undefined

Capture

Initialisation options

I'm using the code from the example, here's the link https://intl-tel-input.com/node_modules/intl-tel-input/examples/gen/default-country-ip.html

jackocnr commented 5 years ago

Is the input empty when you init the plugin?

And can you confirm that your geoIpLookup function is passing "id" into the callback?

fransiscushermanto commented 5 years ago

Yeah, it is passing "id" but on the elements it is still undefined. Emm, i show the country code using my code is that the problem, i put the value of dialCode to my input?

jackocnr commented 5 years ago

As per the docs for initialCountry:

Note that the "auto" option will not update the country selection if the input already contains a number.

fransiscushermanto commented 5 years ago

So how can i show the dial code? Is there any function to show dial code?

jackocnr commented 5 years ago

If you want to show the dial code at all times, I'd recommend the separateDialCode option.

Can you confirm that if you leave the input empty then it is setting the flag correctly?

fransiscushermanto commented 5 years ago

Yeah it works when input is empty, but my client want to show the dialcode. Is there any example of seperateDialCode function? so i can see how the function show the dialcode

jackocnr commented 5 years ago

Yeah it works when input is empty

Great. I'll close this issue then.

Is there any example of seperateDialCode function?

There is a screenshot in the readme. Just read about it in the readme and give it a try - it's fairly simple.

fransiscushermanto commented 5 years ago

Capture

or

Capture

Is it true? cause i'have tried to do both but nothing change.. I have read Readme but there is no example for it on how using it.. can you please show me how to use it?

e10a commented 5 years ago

@fransiscushuang this is working for me (note: separate not seperate)

separateDialCode: true,

Hope this solves that particular issue!