Methods

This page lists the plugin’s public API methods.

Contents

Instance Methods

These methods are called on the plugin instance. The examples below all use a variable named iti for the instance — how you get hold of it depends on the integration:

Important

Methods that require the utils script (e.g. getNumber, getNumberType, isValidNumber) will throw if called before utils have finished loading. Always await iti.promise first:

await iti.promise;
const number = iti.getNumber();
destroy

Type: () => void

Remove the plugin from the input, and unbind any event listeners.

iti.destroy();
getExtension

Type: () => string

Get the extension from the current number. Requires the utils script to be loaded.

const extension = iti.getExtension();

For example, if the input value was "(702) 555-5555 ext. 1234", this would return "1234".

getNumber

Type: (format?: number) => string

Get the current number in the given format (defaults to E.164 standard). The different formats are available in the enum intlTelInput.utils.numberFormat. Requires the utils script to be loaded. Note that even if nationalMode is enabled, this can still return a full international number. Also note that this method expects a valid number, and so should only be used after validation.

const number = iti.getNumber(); // format defaults to E.164 e.g. "+17024181234"
// or
const { INTERNATIONAL } = intlTelInput.utils.numberFormat;
const number = iti.getNumber(INTERNATIONAL); // e.g. "+1 702-418-1234"
getNumberType

Type: () => number

Get the type (fixed-line/mobile/toll-free, etc) of the current number. Requires the utils script to be loaded. It returns an integer, which you can match against the various options in the enum intlTelInput.utils.numberType.

const numberType = iti.getNumberType();
const { MOBILE, FIXED_LINE_OR_MOBILE } = intlTelInput.utils.numberType;
if (numberType === MOBILE || numberType === FIXED_LINE_OR_MOBILE) {
    // is (or could be) a mobile number
}

Note that in some countries (e.g. the US) there’s no way to differentiate between fixed-line and mobile numbers, so in those cases it will return FIXED_LINE_OR_MOBILE — hence the need to check for both values above.

getSelectedCountryData

Type: () => { name: string, iso2: string, dialCode: string } | null

Get the country data for the currently selected country.

const countryData = iti.getSelectedCountryData();

Returns something like this:

{
  name: "Afghanistan",
  iso2: "af",
  dialCode: "93"
}

Returns null if no country is currently selected (e.g. the empty/globe state).

getValidationError

Type: () => number

Get more information about an invalid number. It returns an integer that matches the intlTelInput.utils.validationError enum, or null if the number is valid. See Deriving a user-facing error message for how to turn the error code into a message. Requires the utils script to be loaded.

isValidNumber

Type: () => boolean

(Note: only returns true for valid mobile and fixed_line numbers by default - see allowedNumberTypes)
Check if the current number is valid based on its length - see example, which should be sufficient for most use cases. See isValidNumberPrecise (advanced) for more precise validation, but the advantage of isValidNumber is that it is much more future-proof, as while countries around the world regularly update their number rules, they rarely change their number lengths. If this method returns false, you can use getValidationError to get more information. Requires the utils script to be loaded. Note: previously named isPossibleNumber.

const isValid = iti.isValidNumber();

Returns: true/false

isValidNumberPrecise

Type: () => boolean

⚠️ ADVANCED
(Note: only returns true for valid mobile and fixed_line numbers by default - see allowedNumberTypes)
Check if the current number is valid using precise matching rules for each country/area code, etc - see example. Note that these rules change each month for various countries around the world, so you need to constantly keep the plugin up-to-date (e.g. via an automated script) else you will start rejecting valid numbers. For a simpler and more future-proof form of validation, see isValidNumber above. If validation fails, you can use getValidationError to get more information. Requires the utils script to be loaded.

const isValid = iti.isValidNumberPrecise();

Returns: true/false

setCountry

Type: (iso2?: string) => void

Change the selected country. It should be rare, if ever, that you need to do this, as the selected country gets updated automatically when calling setNumber and passing a number including an international dial code, which is the recommended usage. Note, you can omit the iso2 code argument to set the country to the default empty (globe) state. Note that if formatOnDisplay is enabled, this will attempt to format the number to either national or international format according to the nationalMode option.

iti.setCountry("gb");
setDisabled

Type: (disabled: boolean) => void

Updates the disabled attribute of both the telephone input and the selected country button. Accepts a boolean value. Use this instead of updating the input’s disabled attribute directly, as this disables the country button too.

iti.setDisabled(true);
setNumber

Type: (number: string) => void

Insert a number, and update the selected country accordingly. Note that if formatOnDisplay is enabled, this will attempt to format the number to either national or international format according to the nationalMode option.

iti.setNumber("+447733123456");
setPlaceholderNumberType

Type: (type: string) => void

Change the placeholderNumberType option.

iti.setPlaceholderNumberType("FIXED_LINE");
setReadonly

Type: (readonly: boolean) => void

Updates the readonly attribute of the telephone input and disables the selected country button. Accepts a boolean value. Use this instead of updating the input’s readonly attribute directly, as this disables the country button too.

iti.setReadonly(true);

Static Methods

attachUtils

Type: (source: () => Promise<unknown>) => Promise<unknown> | null

An alternative to the loadUtils option (which loads the utils.js script at plugin initialisation time), this method allows you to load the utils at your time of choosing. See Loading The Utils Script for more information. A Promise object is returned so you can await it to know when it’s finished. Note: This method should only be called once per page - it will return null if utils have already been loaded, or are in the process of being loaded.

const loadUtils = () => import("/dist/js/utils.js");
await intlTelInput.attachUtils(loadUtils);
// you can now call methods that use utils
getCountryData

Type: () => Array<{ name: string, iso2: string, dialCode: string }>

Retrieve the plugin’s country data - either to re-use elsewhere (e.g. to generate your own country dropdown), or alternatively, you could use it to modify the country data. Note that any modifications must be done before initialising the plugin.

const countryData = intlTelInput.getCountryData();

Returns an array of country objects:

[{
  name: "Afghanistan",
  iso2: "af",
  dialCode: "93"
}, ...]
getInstance

Type: (input: HTMLInputElement) => Iti | null

After initialising the plugin, you can always access the instance again using this method, by just passing in the relevant input element.

const input = document.querySelector('#phone');
const iti = intlTelInput.getInstance(input);
iti.isValidNumber(); // etc