9

I am using the demo code for intlTelInput provided here

I am able to get the dial code using following

var intlNumber = $("#phone").intlTelInput("getSelectedCountryData");

But i am not able to get the full number i.e. "dialcode + number entered by user" from the #phone text box.

Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
Sachin Pakale
  • 292
  • 2
  • 4
  • 19

5 Answers5

16

Use the getnumber option, so in your case:

var intlNumber = $("#phone").intlTelInput("getNumber");

^ This will give you the entire number (country code + phone number)

See docs here.

Khaled
  • 907
  • 1
  • 8
  • 18
4

You have to use the built-in methods for getting the full number but you must add the utilsScript

 $("#phone").intlTelInput(
    { 
      //Other configurations,
      //Other configurations,
      utilsScript : "/path/to/build/utils.js",

    });

in the initialization for the intlTelInput as stated here Intl-Tel-Phone Docs

and then you can get the full number by using

$("#YourInputElementID").intlTelInput("getNumber");
Nafiu Lawal
  • 447
  • 1
  • 7
  • 16
0

The full_phone is gonna do the trick.
It will append a hidden field with full number when submitting the form.

var input = document.querySelector("#phone");
window.intlTelInput(input, {
    hiddenInput: "full_phone",
    utilsScript: "../../build/js/utils.js" 
});

updated: if the above trick didn't work then don't worry you can fix it by putting a hidden input field before the phone input field.

HTML:

var input = document.querySelector("#phone"),
  hidden= document.querySelector("#hidden");

var iti = window.intlTelInput(input, {
  nationalMode: true,
  utilsScript: "../../build/js/utils.js" 
});

var handleChange = function() {
hidden.value = iti.getNumber()
};

// listen to "keyup", but also "change" to update when the user selects a country

input.addEventListener('change', handleChange);
input.addEventListener('keyup', handleChange)
SalluBhai
  • 1
  • 5
  • what are you doing? There is already a hidden input with the formatted and cleaned number, just use the method provided by the plugin – El Gucs Mar 24 '23 at 00:35
0

Submitting form with ajax . This worked for me. I added the hiddenInput : "full_phone" and change the even listener from "submit" to "change" in the intlTelInput.js file

 var iti = document.querySelector("#phone");
  window.intlTelInput(iti, {
    separateDialCode: true,
    hiddenInput : "full_phone",
    utilsScript: "../../build/js/utils.js",
  });

In the intlTelInput.js file

 if (this.telInput.form) this.telInput.form.addEventListener("change", this._handleHiddenInputSubmit);
Isaac
  • 1
  • 2
0

Another option is to simply use getInstance and then getNumber

var input = document.querySelector("#phone"),
window.intlTelInput(input, {
      utilsScript : "/path/to/build/utils.js",
  });
window.intlTelInputGlobals.getInstance(input).getNumber();
palamunder
  • 2,555
  • 1
  • 19
  • 20