v22.18.0 release notes

PhoneInput

PhoneInput enters an international phone number: a searchable list of countries with their flag and dial code, and a number input formatted as you type. The value is an E.164 string, the country follows the dial code of a typed or pasted number, and the built-in metadata checks the number without any dependency, with libphonenumber-js as an optional full validator.

View as .md
import { PhoneInput } from '@exhaustive-ui/core/phoneinput';

PhoneInput is bound with ngModel. Pick a country in the searchable list, by its name or its dial code, and type the number: it is formatted as you type, without moving the caret, and the value is the number in E.164 format, ready to store or to send to an SMS API. Typing or pasting a number that starts with + or 00 selects the country of its dial code.

Value: null

defaultCountry is selected while country is not set; [(country)] is two-way and follows the dial code of the number typed, pasted or written. preferredCountries are listed first, in their order, above a separator, and onlyCountries or excludeCountries limit the list. The names come from Intl.DisplayNames in the locale of the component, the lang of the document by default, with English as the fallback; the flags are emoji, so no image is loaded.

Country:

valueFormat sets the format of the value: e164 (the default, +33612345678), international (+33 6 12 34 56 78) or national (06 12 34 56 78). The component reads any of them back; a national value is read in the selected country. The same helpers are exported for your own code: phoneInputFormat, phoneInputParse and phoneInputValidate.

Value: +33612345678
Value: +33612345678
Value: +33612345678

The built-in validation is light: it checks the dial code, the length of the national number for its country and, for about fifty countries, its leading digits. It needs no dependency and works during server-side rendering, but it cannot tell whether a number is allocated, nor whether it is a mobile. valid() and validation() give the result, and the component adds a phoneNumber error, with its reason, to its form control; an empty value is left to required. Set validateNumber to false to keep the field valid whatever the number.

For a full validation, install libphonenumber-js (MIT), an optional peer dependency, and pass a validator. It is loaded with a dynamic import, in the browser only, when the component is displayed; the built-in check is used until then. The max metadata also knows the type of the numbers, so types can accept mobiles only, for SMS.

npm install libphonenumber-js
Not valid: tooShort
Try 06 12 34 56 78 and 01 23 45 67 89

The country template replaces the content of a country in the list, and selectedcountry the content of the selected country. Their context gives the country (code, localized name, dialCode, flag and preferred) and whether it is selected. Keep the text of the selected country in the accessible name of the Select, which is the country label followed by the name and the dial code: show the name or the dial code, not another text.

A floating label appears on top of the field. Since the country and its dial code are always shown, the label stays floated. Point the label at inputId. Visit FloatLabel documentation for more information.

IftaLabel is used to create infield top aligned labels. Visit IftaLabel documentation for more information.

PhoneInput provides small and large sizes as alternatives to the base, applied to the country Select and to the number input.

Specify the variant property as filled to display the component with a higher visual emphasis than the default outlined style.

The fluid prop makes the component take up the full width of its container when set to true; the number input takes the space left by the country Select.

When disabled is present, the country and the number cannot be changed nor focused. With readonly, they can be focused and read, but not changed.

The invalid state is applied using the invalid property to indicate failed validation, here while the number is not valid.

The value is null while the field is empty, so required works as on the other fields, and the component adds a phoneNumber error when the number is not valid. Link the message with ariaDescribedBy so that it is read with the field.

PhoneInput can also be used with reactive forms, through the formControlName property. The phoneNumber error is added by the component itself; for a control that is not bound to a PhoneInput, such as a value imported from a file, use the phoneNumberValidator() function.

PhoneInput can also be used with Angular Signal Forms. The [formField] directive binds the component to a string field, and the number check of the component shows up in the errors of the field with the phoneNumber kind. The field also sets the invalid and touched states of the component, so it is styled as invalid once the field is touched or the form is submitted. To check a phone number in a schema, outside the component, use phoneInputValidate(value).valid in a validate rule.

Screen Reader

The country is a Select: an element with the combobox role and a listbox popup with a search field. Its accessible name is the countryLabel followed by the selected country and its dial code, such as Country: France +33, so it contains the dial code shown; the flags are hidden from screen readers and each option is read as its name and dial code.

The number is an input of type tel with inputmode="tel", which opens the phone keypad on mobile devices, and autocomplete="tel", so that the browser can fill in a saved number, whose dial code then selects the country. Label it with a label element pointing at inputId, with ariaLabelledBy or with ariaLabel; without any of them it is named by the numberLabel text. Link an error message with ariaDescribedBy; aria-invalid follows the invalid state. The number is always written left to right, also in a right-to-left page.

A polite live region announces the country selected when it is detected from a number typed or pasted, and that the number is not valid when the focus leaves an invalid number. All these texts can be replaced, or translated, with the labels input; an empty invalidMessage turns the announcement off, for example when your error message already has the alert role.

<label for="phone">Phone</label>
<p-phone-input inputId="phone" ariaDescribedBy="phone-help" />
<small id="phone-help">We send a code to this number.</small>

<p-phone-input ariaLabel="Tรฉlรฉphone" locale="fr" [labels]="{ countryLabel: 'Pays', searchLabel: 'Rechercher un pays', countryDetectedMessage: '{0} sรฉlectionnรฉ' }" />

Country Select Keyboard Support

KeyFunction
tabMoves the focus from the country Select to the number input, then out of the component.
enter / space / down arrowOpens the list of countries and moves the focus to its search field.
any characterIn the search field, filters the countries by name, ISO code or dial code.
up arrow / down arrowMoves the active option.
home / endMoves the active option to the first or the last country.
enterSelects the active country and closes the list.
escapeCloses the list and moves the focus back to the Select.

Number Input Keyboard Support

KeyFunction
digitsAre added at the caret; the separators are placed as you type and the caret stays after the same digit. Other characters are dropped.
+At the start of the input, starts an international number: once its dial code is complete, the country is selected and the dial code moves to the Select.
backspace / deleteDeletes the digit before or after the caret, also when the caret is next to a separator.