-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
utils.js
utils.js (found in build/js/) is a custom build of Google's libphonenumber library. It provides the following methods/enums, all namespaced under intlTelInput.utils
, which are used by the plugin, but can also be used directly by you if needed.
Note: countryCode
is optional (you can pass null
) if number
is a full international number.
formatNumber(number, countryCode, format)
Format the given number according to the given format (a numberFormat
enum)
var formattedNumber = intlTelInput.utils.formatNumber("07733123456", "gb", intlTelInput.utils.numberFormat.INTERNATIONAL);
// "+44 7733 123456"
getExampleNumber(countryCode, isNational, numberType)
Get an example number for the given country. isNational
is a boolean, numberType is a numberType
enum.
var exampleNumber = intlTelInput.utils.getExampleNumber("gb", true, intlTelInput.utils.numberType.FIXED_LINE);
// "0121 234 5678"
getNumberType(number, countryCode)
Get the type of the given number (returns a numberType
enum).
var numberType = intlTelInput.utils.getNumberType("07733123456", "gb");
switch (numberType) {
case intlTelInput.utils.numberType.MOBILE:
// do something
...
}
getValidationError(number, countryCode)
If a number is invalid, then you can call this to get the reason (returns a validationError
enum).
var validationError = intlTelInput.utils.getValidationError("07733123455555555", "gb");
switch (validationError) {
case intlTelInput.utils.validationError.TOO_LONG:
// do something
...
}
isValidNumber(number, countryCode)
Return a boolean for if the given number is valid. This is a very accurate validation check (with specific rules for each dial code etc). Note that these valid number rules change each month for various countries around the world, so you need to be careful to keep the plugin up-to-date else you will start rejecting valid numbers. Alternatively, see isPossibleNumber
below.
var isValidNumber = intlTelInput.utils.isValidNumber("07733123455555555", "gb");
// false
isPossibleNumber(number, countryCode)
Return a boolean for if the given number is possible. This is a much simpler form of validation that only checks the length of the number but should be sufficient for most use cases. This approach to validation is much more future-proof as countries very rarely change the length of their phone numbers.
var isPossibleNumber = intlTelInput.utils.isPossibleNumber("07733123455555555", "gb");
// false
See the source file for more info - the enums are towards the end.
numberFormat
e.g. INTERNATIONAL
, NATIONAL
numberType
e.g. FIXED_LINE
, MOBILE
validationError
e.g. INVALID_COUNTRY_CODE
, TOO_SHORT