Lion Logo Lion Fundamentals Guides Components Blog Toggle darkmode

Input Tel: Use Cases

Regions: some context

Say we have the following telephone number from Madrid, Spain: +34919930432.

It contains a country code (34), an area code (91) and a dial code (+34 91). Input Tel interprets phone numbers based on their region code: a two character long region representation('ES' in the telephone number above).

The table below lists all possible regions worldwide. When allowed regions are not configured, all of them will be supported as values of Input Tel.

Region codes
country name region code country code
Ascension Island AC 247
Andorra AD 376
United Arab Emirates AE 971
Afghanistan AF 93
Antigua & Barbuda AG 1
Anguilla AI 1
Albania AL 355
Armenia AM 374
Angola AO 244
Argentina AR 54
American Samoa AS 1
Austria AT 43
Australia AU 61
Aruba AW 297
ร…land Islands AX 358
Azerbaijan AZ 994
Bosnia & Herzegovina BA 387
Barbados BB 1
Bangladesh BD 880
Belgium BE 32
Burkina Faso BF 226
Bulgaria BG 359
Bahrain BH 973
Burundi BI 257
Benin BJ 229
St. Barthรฉlemy BL 590
Bermuda BM 1
Brunei BN 673
Bolivia BO 591
Caribbean Netherlands BQ 599
Brazil BR 55
Bahamas BS 1
Bhutan BT 975
Botswana BW 267
Belarus BY 375
Belize BZ 501
Canada CA 1
Cocos (Keeling) Islands CC 61
Congo - Kinshasa CD 243
Central African Republic CF 236
Congo - Brazzaville CG 242
Switzerland CH 41
Cรดte dโ€™Ivoire CI 225
Cook Islands CK 682
Chile CL 56
Cameroon CM 237
China CN 86
Colombia CO 57
Costa Rica CR 506
Cuba CU 53
Cape Verde CV 238
Curaรงao CW 599
Christmas Island CX 61
Cyprus CY 357
Czechia CZ 420
Germany DE 49
Djibouti DJ 253
Denmark DK 45
Dominica DM 1
Dominican Republic DO 1
Algeria DZ 213
Ecuador EC 593
Estonia EE 372
Egypt EG 20
Western Sahara EH 212
Eritrea ER 291
Spain ES 34
Ethiopia ET 251
Finland FI 358
Fiji FJ 679
Falkland Islands (Islas Malvinas) FK 500
Micronesia FM 691
Faroe Islands FO 298
France FR 33
Gabon GA 241
United Kingdom GB 44
Grenada GD 1
Georgia GE 995
French Guiana GF 594
Guernsey GG 44
Ghana GH 233
Gibraltar GI 350
Greenland GL 299
Gambia GM 220
Guinea GN 224
Guadeloupe GP 590
Equatorial Guinea GQ 240
Greece GR 30
Guatemala GT 502
Guam GU 1
Guinea-Bissau GW 245
Guyana GY 592
Hong Kong HK 852
Honduras HN 504
Croatia HR 385
Haiti HT 509
Hungary HU 36
Indonesia ID 62
Ireland IE 353
Israel IL 972
Isle of Man IM 44
India IN 91
British Indian Ocean Territory IO 246
Iraq IQ 964
Iran IR 98
Iceland IS 354
Italy IT 39
Jersey JE 44
Jamaica JM 1
Jordan JO 962
Japan JP 81
Kenya KE 254
Kyrgyzstan KG 996
Cambodia KH 855
Kiribati KI 686
Comoros KM 269
St. Kitts & Nevis KN 1
North Korea KP 850
South Korea KR 82
Kuwait KW 965
Cayman Islands KY 1
Kazakhstan KZ 7
Laos LA 856
Lebanon LB 961
St. Lucia LC 1
Liechtenstein LI 423
Sri Lanka LK 94
Liberia LR 231
Lesotho LS 266
Lithuania LT 370
Luxembourg LU 352
Latvia LV 371
Libya LY 218
Morocco MA 212
Monaco MC 377
Moldova MD 373
Montenegro ME 382
St. Martin MF 590
Madagascar MG 261
Marshall Islands MH 692
North Macedonia MK 389
Mali ML 223
Myanmar (Burma) MM 95
Mongolia MN 976
Macao MO 853
Northern Mariana Islands MP 1
Martinique MQ 596
Mauritania MR 222
Montserrat MS 1
Malta MT 356
Mauritius MU 230
Maldives MV 960
Malawi MW 265
Mexico MX 52
Malaysia MY 60
Mozambique MZ 258
Namibia NA 264
New Caledonia NC 687
Niger NE 227
Norfolk Island NF 672
Nigeria NG 234
Nicaragua NI 505
Netherlands NL 31
Norway NO 47
Nepal NP 977
Nauru NR 674
Niue NU 683
New Zealand NZ 64
Oman OM 968
Panama PA 507
Peru PE 51
French Polynesia PF 689
Papua New Guinea PG 675
Philippines PH 63
Pakistan PK 92
Poland PL 48
St. Pierre & Miquelon PM 508
Puerto Rico PR 1
Palestine PS 970
Portugal PT 351
Palau PW 680
Paraguay PY 595
Qatar QA 974
Rรฉunion RE 262
Romania RO 40
Serbia RS 381
Russia RU 7
Rwanda RW 250
Saudi Arabia SA 966
Solomon Islands SB 677
Seychelles SC 248
Sudan SD 249
Sweden SE 46
Singapore SG 65
St. Helena SH 290
Slovenia SI 386
Svalbard & Jan Mayen SJ 47
Slovakia SK 421
Sierra Leone SL 232
San Marino SM 378
Senegal SN 221
Somalia SO 252
Suriname SR 597
South Sudan SS 211
Sรฃo Tomรฉ & Prรญncipe ST 239
El Salvador SV 503
Sint Maarten SX 1
Syria SY 963
Eswatini SZ 268
Tristan da Cunha TA 290
Turks & Caicos Islands TC 1
Chad TD 235
Togo TG 228
Thailand TH 66
Tajikistan TJ 992
Tokelau TK 690
Timor-Leste TL 670
Turkmenistan TM 993
Tunisia TN 216
Tonga TO 676
Turkey TR 90
Trinidad & Tobago TT 1
Tuvalu TV 688
Taiwan TW 886
Tanzania TZ 255
Ukraine UA 380
Uganda UG 256
United States US 1
Uruguay UY 598
Uzbekistan UZ 998
Vatican City VA 39
St. Vincent & Grenadines VC 1
Venezuela VE 58
British Virgin Islands VG 1
U.S. Virgin Islands VI 1
Vietnam VN 84
Vanuatu VU 678
Wallis & Futuna WF 681
Samoa WS 685
Kosovo XK 383
Yemen YE 967
Mayotte YT 262
South Africa ZA 27
Zambia ZM 260
Zimbabwe ZW 263

Active region

The active region (accessible via readonly accessor .activeRegion) determines how validation and formatting should be applied. It is dependent on the following factors:

What follows from the list above is that .activeRegion can change dynamically, after a value change in the text box by the user (or when locales or allowed regions would be changed by the Application Developer).

How active region is computed

The following heuristic will be applied:

  1. check for allowed regions: if one region defined in .allowedRegions, use it.
  2. check for user input: try to derive active region from user input
  3. check for locale: try to get the region from locale (html[lang] attribute)
export const heuristic = () => {
  loadDefaultFeedbackMessages();

  const initialAllowedRegions = ['CN', 'ES'];
  const [inputTelRef, outputRef, selectRef] = [createRef(), createRef(), createRef()];

  const setDerivedActiveRegionScenario = (
    scenarioToSet,
    inputTel = inputTelRef.value,
    output = outputRef.value,
  ) => {
    if (scenarioToSet === 'only-allowed-region') {
      // activeRegion will be the top allowed region, which is 'NL'
      inputTel.modelValue = undefined;
      inputTel.allowedRegions = ['NL']; // activeRegion will always be the only option
      output.innerText = '.activeRegion (NL) is only allowed region';
    } else if (scenarioToSet === 'user-input') {
      // activeRegion will be based on phone number => 'BE'
      inputTel.allowedRegions = ['NL', 'BE', 'DE'];
      inputTel.modelValue = '+3261234567'; // BE number
      output.innerText = '.activeRegion (BE) is derived (since within allowedRegions)';
    } else if (scenarioToSet === 'locale') {
      localize.locale = 'en-GB';
      // activeRegion will be `html[lang]`
      inputTel.modelValue = undefined;
      inputTel.allowedRegions = undefined;
      output.innerText = `.activeRegion (${inputTel._langIso}) set to locale when inside allowed or all regions`;
    } else {
      output.innerText = '';
    }
  };
  return html`
    <select
      aria-label="Set scenario"
      @change="${({ target }) => setDerivedActiveRegionScenario(target.value)}"
    >
      <option value="">--- select scenario ---</option>
      <option value="only-allowed-region">1. only allowed region</option>
      <option value="user-input">2. user input</option>
      <option value="locale">3. locale</option>
    </select>
    <output style="display:block; min-height: 1.5em;" id="myOutput" ${ref(outputRef)}></output>
    <lion-input-tel
      ${ref(inputTelRef)}
      @model-value-changed="${({ detail }) => {
        if (detail.isTriggeredByUser && selectRef.value) {
          selectRef.value.value = '';
        }
      }}"
      name="phoneNumber"
      label="Active region"
      .allowedRegions="${initialAllowedRegions}"
    ></lion-input-tel>
    <h-output
      .show="${[
        'activeRegion',
        {
          name: 'all or allowed regions',
          processor: el => JSON.stringify(el._allowedOrAllRegions),
        },
        'modelValue',
      ]}"
      .readyPromise="${PhoneUtilManager.loadComplete}"
    ></h-output>
  `;
};

Allowed regions

.allowedRegions is an array of one or more region codes. Once it is configured, validation and formatting will be restricted to those values that are present in this list.

Note that for InputTelDropdown, only allowed regions will be shown in the dropdown list.

export const allowedRegions = () => {
  loadDefaultFeedbackMessages();
  return html`
    <lion-input-tel
      label="Allowed regions 'NL', 'BE', 'DE'"
      help-text="Type '+31'(NL), '+32'(BE) or '+49'(DE) and see how activeRegion changes"
      .allowedRegions="${['NL', 'BE', 'DE']}"
      .modelValue="${'+31612345678'}"
      name="phoneNumber"
    ></lion-input-tel>
    <h-output
      .show="${['modelValue', 'activeRegion']}"
      .readyPromise="${PhoneUtilManager.loadComplete}"
    ></h-output>
  `;
};
Type '+31'(NL), '+32'(BE) or '+49'(DE) and see how activeRegion changes

Restrict to one region

When one allowed region is configured, validation and formatting will be restricted to just that region (that means that changes of the region via viewValue won't have effect).

export const oneAllowedRegion = () => {
  loadDefaultFeedbackMessages();
  return html`
    <lion-input-tel
      label="Only allowed region 'DE'"
      help-text="Restricts validation / formatting to one region"
      .allowedRegions="${['DE']}"
      .modelValue="${'+31612345678'}"
      name="phoneNumber"
    ></lion-input-tel>
    <h-output
      .show="${['modelValue', 'activeRegion', 'validationStates']}"
      .readyPromise="${PhoneUtilManager.loadComplete}"
    ></h-output>
  `;
};
Restricts validation / formatting to one region

Format

Format strategy

Determines what the formatter output should look like. Formatting strategies as provided by awesome-phonenumber / google-libphonenumber.

Possible values:

strategyoutput
e164+46707123456
international+46 70 712 34 56
national070-712 34 56
significant707123456
rfc3966tel:+46-70-712-34-56

Also see:

export const formatStrategy = () => {
  loadDefaultFeedbackMessages();
  const inputTel = createRef();
  return html`
    <select @change="${({ target }) => (inputTel.value.formatStrategy = target.value)}">
      <option value="e164">e164</option>
      <option value="international">international</option>
      <option value="national">national</option>
      <option value="significant">significant</option>
      <option value="rfc3966">rfc3966</option>
    </select>
    <lion-input-tel
      ${ref(inputTel)}
      label="Format strategy"
      help-text="Choose a strategy above"
      .modelValue="${'+46707123456'}"
      format-strategy="national"
      name="phoneNumber"
    ></lion-input-tel>
    <h-output
      .show="${['modelValue', 'formatStrategy']}"
      .readyPromise="${PhoneUtilManager.loadComplete}"
    ></h-output>
  `;
};
Choose a strategy above

Format country code style

You can also style the country code with parentheses.

export const formatCountryCodeStyle = () => {
  loadDefaultFeedbackMessages();
  const inputTel = createRef();
  return html`
    <select @change="${({ target }) => (inputTel.value.formatStrategy = target.value)}">
      <option value="e164">e164</option>
      <option value="international">international</option>
      <option value="rfc3966">rfc3966</option>
    </select>
    <lion-input-tel
      ${ref(inputTel)}
      label="Format strategy"
      help-text="Choose a strategy above"
      .modelValue="${'+46707123456'}"
      format-country-code-style="parentheses"
      name="phoneNumber"
    ></lion-input-tel>
    <h-output
      .show="${['modelValue', 'formatStrategy']}"
      .readyPromise="${PhoneUtilManager.loadComplete}"
    ></h-output>
  `;
};
Choose a strategy above

Live format

Type '6' in the example below to see how the phone number is formatted during typing.

See awesome-phonenumber documentation

export const liveFormat = () => {
  loadDefaultFeedbackMessages();
  return html`
    <lion-input-tel
      label="Realtime format on user input"
      help-text="Partial numbers are also formatted"
      .modelValue="${new Unparseable('+31')}"
      format-strategy="international"
      live-format
      name="phoneNumber"
    ></lion-input-tel>
  `;
};
Partial numbers are also formatted

Active phone number type

The readonly acessor .activePhoneNumberType outputs the current phone number type, based on the textbox value.

Possible types: fixed-line, fixed-line-or-mobile, mobile, pager, personal-number, premium-rate, shared-cost, toll-free, uan, voip, unknown

Also see:

export const activePhoneNumberType = () => {
  loadDefaultFeedbackMessages();
  return html`
    <lion-input-tel
      label="Active phone number type"
      .modelValue="${'+31612345678'}"
      format-strategy="international"
      name="phoneNumber"
    ></lion-input-tel>
    <h-output
      .show="${['activePhoneNumberType']}"
      .readyPromise="${PhoneUtilManager.loadComplete}"
    ></h-output>
  `;
};