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:
JavaScript plugin: const iti = intlTelInput(input, options) — directly from the initialisation call.
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();
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"
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.
(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.
⚠️ 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.
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.
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.
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.
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.
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
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.