# Phone number input

The **phone number input** allows a user to enter a phone number by selecting the country (international area code) from a dropdown list and entering the rest of the number in a text input field.

## Usage

![Phone number input](<../images/phone-input.png>)

### When to use

- For allowing users to enter or update a phone number.
- When validation and formatting of international phone numbers are required.

### Best practices

- Ensure the input field is wide enough to accommodate different international phone number formats.
- Use smart defaults, such as showing the country code and phone number format based on the user's location.
- When collecting non-phone number data, a standard [input](<https://element.siemens.io/components/forms-inputs/input/>) component should be used.

## Design

### Elements

![Phone number input - Elements](<../images/phone-input-elements.png>)

> 1\. Label (optional) 2. Content/placeholder text, 3. Container, 4. Menu, 5. Country code with flag.

#### Label (optional)

Effective form labeling helps users understand what information to enter.

![Phone number input - Label](<../images/phone-input-label.png>)

- Keep labels short and clear (should not be wider than the input field).
- A mandatory field is marked with an **`*`-asterisk.**

### Interaction states

![Number input - States](<../images/phone-input-states.png>)

### Validation

Real-time validation ensures that the data being entered is within a specific range or follows certain rules, preventing errors and ensuring the integrity of the data.

![Phone number input - Validation](<../images/phone-input-validation.png>)

## Code

### Usage

```
import { SiPhoneNumberInputComponent } from '@siemens/element-ng/phone-number';

@Component({
  imports: [SiPhoneNumberInputComponent, ...]
})
```

> [!NOTE]
>
> **Required Packages**
>
> - [google-libphonenumber](<https://www.npmjs.com/package/google-libphonenumber>)
> - [@types/google-libphonenumber](<https://www.npmjs.com/package/@types/google-libphonenumber>)
> - [flag-icons](<https://www.npmjs.com/package/flag-icons>)
>
> The versions of the required packages are defined in the peer-dependencies of [element-ng](<https://github.com/siemens/element/blob/main/projects/element-ng/package.json>). Together with the types package, they are also part of the [package.json of the demo app](<https://github.com/siemens/element/blob/main/package.json>).

- Ensure to import the `node_modules/flag-icons/css/flag-icons.css` to the `angular.json` styles configuration as follows:

```
          "styles": [
              "node_modules/flag-icons/css/flag-icons.css",
              ...
          ]
```

- Set the `supportedCountries` input array to define the available list of countries. The countries are defined in [ISO-3166-2](<https://en.wikipedia.org/wiki/ISO_3166-2>) format.
- To display all the available countries, don't add `supportedCountries` input.
- Use the `defaultCountry` [ISO-3166-2](<https://en.wikipedia.org/wiki/ISO_3166-2>) input to select a default.
- Set `validatePhoneNumber` input to enable a validation service and show status information in the form control.
- Use the `placeholderForSearch` input for a custom placeholder text of the country search.
- The `selectCountryAriaLabel` input sets the ARIA label for the country dropdown.
- The `phoneNumberAriaLabel` input sets the ARIA label for the phone number input.
- All changes are emitted through the `valueChange` output.

#### Example

Component:

```typescript
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { SiFormItemComponent } from '@siemens/element-ng/form';
import { PhoneDetails, SiPhoneNumberInputComponent } from '@siemens/element-ng/phone-number';

@Component({
  selector: 'app-sample',
  imports: [CommonModule, SiPhoneNumberInputComponent, ReactiveFormsModule, SiFormItemComponent],
  templateUrl: './si-phone-number-input.html'
})
export class SampleComponent {
  lastEventValue!: PhoneDetails;
  form = new FormGroup({
    phoneNumber: new FormControl()
  });
}
```

Template:

```html
<form class="p-5" [formGroup]="form">
  <div class="row">
    <div class="col-sm-6 col-lg-4">
      <si-form-item label="Phone number:">
        <si-phone-number-input
          class="form-control"
          formControlName="phoneNumber"
          placeholderForSearch="FORM.SEARCH"
          selectCountryAriaLabel="FORM.SELECT_COUNTRY"
          defaultCountry="CH"
          [supportedCountries]="['AU', 'CH', 'DE', 'FK', 'IN', 'US']"
          (valueChange)="lastEventValue = $event"
        />
      </si-form-item>
    </div>
  </div>
  <hr />
  <div>
    <span class="si-h5">Last emitted change event: </span>
    <pre>{{ lastEventValue | json }}</pre>
  </div>
  <div>
    <span class="si-h5">Current form value: </span>
    <pre>{{ form.controls.phoneNumber.value | json }}</pre>
  </div>
  <div>
    <span class="si-h5">Validation Error: </span>
    <pre>{{ form.controls.phoneNumber.errors | json }}</pre>
  </div>
</form>
```

### [SiPhoneNumberInputComponent](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L86-L468>)

**Kind:** component

**Selector:** `si-phone-number-input`

```
import { SiPhoneNumberInputComponent } from '@siemens/element-ng/phone-number';
```

```
component SiPhoneNumberInputComponent implements ControlValueAccessor, Validator, OnChanges, SiFormItemControl
```

#### Input Properties

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [country](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L108-L108>) | `string \| undefined` |  | ISO\_3166-2 Code of the selected country. |
| [defaultCountry](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L113-L113>) | `string \| undefined` |  | ISO\_3166-2 Code of the country which shall be used on form-control reset. |
| [disabled](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L188-L188>) | `boolean` | `false` | Whether the input is disabled. |
| [errormessageId](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L210-L210>) | `string` | `` `${this.id()}-errormessage` `` | This ID will be bound to the `aria-describedby` attribute of the phone-number-input. Use this to reference the element containing the error message(s) for the phone-number-input. It will be picked by the [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>) if the phone-number-input is used inside a form item.<br><br>Implements [SiFormItemControl](<index.html#t:SiFormItemControl>) |
| [id](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L103-L103>) | `string` | `` `__si-phone-number-input-${SiPhoneNumberInputComponent.idCounter++}` `` | Unique identifier.<br><br>Implements [SiFormItemControl](<index.html#t:SiFormItemControl>) |
| [labelledby](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L180-L180>) | `string` | `` `${this.id()}-label` `` | ID of the element that labels the phone number input.<br><br>Implements [SiFormItemControl](<index.html#t:SiFormItemControl>) |
| [phoneNumberAriaLabel](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L163-L165>) | `TranslatableString` | `` $localize`:@@SI_PHONE_NUMBER_INPUT.PHONE_NUMBER_INPUT_LABEL:Enter phone number` `` | Text for the phone number input aria-label attribute. |
| [placeholderForSearch](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L130-L132>) | `TranslatableString` | `` $localize`:@@SI_PHONE_NUMBER_INPUT.SEARCH_PLACEHOLDER:Search` `` | Placeholder text for country search input. |
| [readonly](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L195-L195>) | `boolean` | `false` | Whether the input can be focused but not edited. |
| [scrollStrategy](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L120-L120>) | `ScrollStrategy` | `defaultConnectedOverlayScrollStrategy()` | Optional CDK scroll strategy used for the country selection overlay. |
| [searchNoResultsFoundLabel](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L141-L143>) | `TranslatableString` | `` $localize`:@@SI_PHONE_NUMBER_INPUT.SEARCH_NO-RESULTS_FOUND:No results found` `` | Text shown when the country search returns no results. |
| [selectCountryAriaLabel](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L152-L154>) | `TranslatableString` | `` $localize`:@@SI_PHONE_NUMBER_INPUT.SELECT_COUNTRY:Select country` `` | Text for the country dropdown aria-label attribute. |
| [supportedCountries](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L170-L170>) | `readonly string[] \| null \| undefined` |  | List of countries in ISO2 format, from which the user is allowed to select one. If no values are provided, the dropdown will show all known countries. |

#### Output Properties

| Name | Type | Description |
| --- | --- | --- |
| [countryChange](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L108-L108>) | `string \| undefined` | ISO\_3166-2 Code of the selected country. |
| [valueChange](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.component.ts#L198-L198>) | `PhoneDetails` | Emits the selected country, formatted phone number, and its validity when the value changes. |

### Types Documentation

### [TranslatableString](<https://github.com/siemens/element/blob/main/projects/element-translate-ng/translate-types/global.types.ts#L18-L18>)

**Kind:** type alias

```
import type { TranslatableString } from '@siemens/element-translate-ng/translate-types';
```

Represents a translatable string. This can either be a translation key, e.g. `ACTIONS.EDIT` that will be automatically translated when displayed on the UI or a pre-translated string, e.g. `Edit`. Equivalent to a normal string in usage and functionality.

```
type TranslatableString = string & Translatable
```

### [PhoneDetails](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.models.ts#L38-L51>)

**Kind:** interface

```
import type { PhoneDetails } from '@siemens/element-ng/phone-number';
```

Describes the phone number, its validity and associated country information.

```
interface PhoneDetails
```

#### Properties

| Name | Type | Description |
| --- | --- | --- |
| [country](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.models.ts#L42-L42>) (optional) | `CountryInfo` | CountryInfo object containing name, countryCode and isoCode of the phone number. |
| [isValid](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.models.ts#L50-L50>) | `boolean` | Confirms if the phone number is valid or not |
| [phoneNumber](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.models.ts#L46-L46>) (optional) | `string` | Phone number entered by the user. |

### [SiFormItemControl](<https://github.com/siemens/element/blob/main/projects/element-ng/form/si-form-item.control.ts#L14-L38>)

**Kind:** interface

```
import type { SiFormItemControl } from '@siemens/element-ng/form';
```

Common interface for form item controls. Controls that should be connected to [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>) must implement this interface and must be provided using the [SI\_FORM\_ITEM\_CONTROL](<../../../api/siemens/element-ng/types/SI_FORM_ITEM_CONTROL/index.html#t:SI_FORM_ITEM_CONTROL>) token.

```
interface SiFormItemControl
```

#### Properties

| Name | Type | Description |
| --- | --- | --- |
| [errormessageId](<https://github.com/siemens/element/blob/main/projects/element-ng/form/si-form-item.control.ts#L34-L34>) (readonly) | `string \| Signal<string>` | Generated ID that the [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>) will assign to the element containing the error messages. Implementations of [SiFormItemControl](<index.html#t:SiFormItemControl>) must generate this ID and assign it to the `aria-describedby` attribute of their focusable elements. |
| [id](<https://github.com/siemens/element/blob/main/projects/element-ng/form/si-form-item.control.ts#L16-L16>) (optional, readonly) | `string \| Signal<string \| undefined>` | The actual id of the control that should be used in the `for` attribute of a label. |
| [isFormCheck](<https://github.com/siemens/element/blob/main/projects/element-ng/form/si-form-item.control.ts#L37-L37>) (optional, readonly) | `boolean` | If the control is meant to be used a checkbox. This affects the label positioning. |
| [labelledby](<https://github.com/siemens/element/blob/main/projects/element-ng/form/si-form-item.control.ts#L27-L27>) (optional, readonly) | `string \| Signal<string \| undefined> \| null` | ID that should be assigned to the label element inside the [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>).<br><br>If the target control is not of type input, select or button, it cannot be referenced by a label. Instead, aria-labelledby must be used to create a reference to a label. In such a case, an implementation of [SiFormItemControl](<index.html#t:SiFormItemControl>) must provide the id of the label in [labelledby](<index.html#t:SiFormItemControl.labelledby>). [SiFormItemComponent](<../../../api/siemens/element-ng/components/SiFormItemComponent/index.html#SiFormItemComponent>) will apply the id to the label so that it can be used inside the control to reference it in the aria-labelledby attribute. |

### [CountryInfo](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.models.ts#L20-L33>)

**Kind:** interface

```
import type { CountryInfo } from '@siemens/element-ng/phone-number';
```

Describes the country information of the phone number.

```
interface CountryInfo
```

#### Properties

| Name | Type | Description |
| --- | --- | --- |
| [countryCode](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.models.ts#L28-L28>) | `number` | The country dial code |
| [isoCode](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.models.ts#L32-L32>) | `string` | The country ISO code. |
| [name](<https://github.com/siemens/element/blob/main/projects/element-ng/phone-number/si-phone-number-input.models.ts#L24-L24>) | `string` | The name of the country. |
