# IP input

## Usage

### When to use

- For allowing users to enter IP-Addresses.
- When validation for IP-Addresses are required.

### Best Practices for IP Input

- Ensure the input field is wide enough to accommodate different IP-Address formats.

## Code

The directives are an enhancement to the HTML input field specifically for IPv4 and IPv6 addresses.

**Features:**

- Validation - Provide forms input validation
- Input Masking - Improved user experience by enforcing correct formatting
- CIDR Support - Optional support for Classless Inter-Domain Routing (CIDR) notation (like 192.168.0.1/24)

### IPv4 address

```
...
import { SiFormItemComponent } from '@siemens/element-ng/form';
import { SiIp4InputDirective } from '@siemens/element-ng/ip-input';

@Component({
  template: `
    <si-form-item label="IPv4 address">
      <input type="text" class="form-control" siIpV4 />
    </si-form-item>`,
  imports: [SiFormItemComponent, SiIp4InputDirective, ...]
})
```

### IPv6 address

```
...
import { SiFormItemComponent } from '@siemens/element-ng/form';
import { SiIp6InputDirective } from '@siemens/element-ng/ip-input';

@Component({
  template: `
    <si-form-item label="IPv6 address">
      <input type="text" class="form-control" siIpV6 />
    </si-form-item>`,
  imports: [SiFormItemComponent, SiIp6InputDirective, ...]
})
```

#### Example

Component:

```typescript
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { Component, inject, signal } from '@angular/core';
import { FormControl, FormGroup, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { SiFormItemComponent } from '@siemens/element-ng/form';
import { SiIp4InputDirective, SiIp6InputDirective } from '@siemens/element-ng/ip-input';
import { LOG_EVENT } from '@siemens/live-preview';

@Component({
  selector: 'app-sample',
  imports: [
    FormsModule,
    ReactiveFormsModule,
    SiIp4InputDirective,
    SiIp6InputDirective,
    SiFormItemComponent
  ],
  templateUrl: './si-ip-input.html'
})
export class SampleComponent {
  protected readonly logEvent = inject(LOG_EVENT);
  protected readonly configForm = new FormGroup({
    cidr: new FormControl(false, { nonNullable: true })
  });
  protected readonly ipV4Placeholder = signal('192.168.0.1');
  protected ipV4address = '';
  protected ipV6address = '';

  constructor() {
    this.configForm.valueChanges.subscribe(value => {
      this.ipV4Placeholder.set(value.cidr ? '192.168.0.1/24' : '192.168.0.1');
    });
  }
  onIpChange(field: string, value: string): void {
    this.logEvent(field, value);
  }
}
```

Template:

```html
<div class="row justify-content-center">
  <div class="col-12 col-md-4 my-6">
    <si-form-item label="IP V4 address">
      <input
        type="text"
        class="form-control"
        siIpV4
        [placeholder]="ipV4Placeholder()"
        [cidr]="configForm.value.cidr"
        [(ngModel)]="ipV4address"
        (ngModelChange)="onIpChange('IPV4', $event)"
      />
    </si-form-item>
  </div>
</div>
<div> Model: {{ ipV4address }} </div>
<div class="row justify-content-center">
  <div class="col-12 col-md-4 my-6">
    <si-form-item label="IP V6 address">
      <input
        type="text"
        class="form-control"
        siIpV6
        placeholder="2001:0DB8:85A3:0000:0000:8A2E:0370:7334"
        [cidr]="configForm.value.cidr"
        [(ngModel)]="ipV6address"
        (ngModelChange)="onIpChange('IPV6', $event)"
      />
    </si-form-item>
  </div>
</div>
<div> Model: {{ ipV6address }} </div>
<div class="card mt-6 e2e-ignore">
  <div class="card-header">Control panel</div>
  <div class="card-body">
    <form [formGroup]="configForm">
      <div class="row">
        <div class="col-sm-3">
          <si-form-item label="Enable CIDR notation e.g. 198.51.100.0/22">
            <input type="checkbox" class="form-check-input" formControlName="cidr" />
          </si-form-item>
        </div>
      </div>
    </form>
  </div>
</div>
```

### [SiIp4InputDirective](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip4-input.directive.ts#L54-L88>)

**Kind:** directive

**Selector:** `input[siIpV4]`

**Exported as:** `siIpV4`

```
import { SiIp4InputDirective } from '@siemens/element-ng/ip-input';
```

Directive for IPv4 address input fields.

Usage:

```ts
import { SiFormItemComponent } from '@siemens/element-ng/form';
import { SiIp4InputDirective } from '@siemens/element-ng/ip-input';

@Component({
  template: `
    <si-form-item label="IPv4 address">
      <input type="text" class="form-control" siIpV4 />
    </si-form-item>
  `,
  imports: [SiFormItemComponent, SiIp4InputDirective, ...]
})
```

```
directive SiIp4InputDirective extends SiIpInputDirective implements ControlValueAccessor, Validator
```

#### Input Properties

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [cidr](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L67-L67>) (inherited) | `boolean` | `false` | Enable CIDR (Classless Inter-Domain Routing) notation.<br><br>Inherited from `SiIpInputDirective` |
| [disabled](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L74-L74>) (inherited) | `boolean` | `false` | Whether the ip address input is disabled.<br><br>Inherited from `SiIpInputDirective` |
| [id](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L61-L61>) (inherited) | `string` | `` `si-ip-input-${SiIpInputDirective.idCounter++}` `` | Inherited from `SiIpInputDirective` |

#### Attributes and Methods

##### registerOnChange

[Source for registerOnChange](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L82-L84>)

**Modifiers:** inherited

Inherited from `SiIpInputDirective`

Implements [ControlValueAccessor](<https://angular.dev/api/forms/ControlValueAccessor>)

```
registerOnChange(fn: any): void
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [fn](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L82-L82>) | `any` |  | The callback function to register |

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)

##### registerOnTouched

[Source for registerOnTouched](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L86-L88>)

**Modifiers:** inherited

Inherited from `SiIpInputDirective`

Implements [ControlValueAccessor](<https://angular.dev/api/forms/ControlValueAccessor>)

```
registerOnTouched(fn: () => void): void
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [fn](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L86-L86>) | `() => void` |  | The callback function to register |

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)

##### setDisabledState

[Source for setDisabledState](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L90-L92>)

**Modifiers:** inherited

Inherited from `SiIpInputDirective`

Implements [ControlValueAccessor](<https://angular.dev/api/forms/ControlValueAccessor>)

```
setDisabledState(isDisabled: boolean): void
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [isDisabled](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L90-L90>) | `boolean` |  | The disabled status to set on the element |

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)

##### writeValue

[Source for writeValue](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L94-L97>)

**Modifiers:** inherited

Inherited from `SiIpInputDirective`

Implements [ControlValueAccessor](<https://angular.dev/api/forms/ControlValueAccessor>)

```
writeValue(value?: string | null): void
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [value](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L94-L94>) (optional) | `string \| null` |  | The new value for the element |

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)

### [SiIp6InputDirective](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip6-input.directive.ts#L54-L92>)

**Kind:** directive

**Selector:** `input[siIpV6]`

**Exported as:** `siIpV6`

```
import { SiIp6InputDirective } from '@siemens/element-ng/ip-input';
```

Directive for IPv6 address input fields.

Usage:

```ts
import { SiFormItemComponent } from '@siemens/element-ng/form';
import { SiIp6InputDirective } from '@siemens/element-ng/ip-input';

@Component({
  template: `
    <si-form-item label="IPv6 address">
      <input type="text" class="form-control" siIpV6 />
    </si-form-item>
  `,
  imports: [SiFormItemComponent, SiIp6InputDirective, ...]
})
```

```
directive SiIp6InputDirective extends SiIpInputDirective implements ControlValueAccessor, Validator
```

#### Input Properties

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [cidr](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L67-L67>) (inherited) | `boolean` | `false` | Enable CIDR (Classless Inter-Domain Routing) notation.<br><br>Inherited from `SiIpInputDirective` |
| [disabled](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L74-L74>) (inherited) | `boolean` | `false` | Whether the ip address input is disabled.<br><br>Inherited from `SiIpInputDirective` |
| [id](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L61-L61>) (inherited) | `string` | `` `si-ip-input-${SiIpInputDirective.idCounter++}` `` | Inherited from `SiIpInputDirective` |

#### Attributes and Methods

##### registerOnChange

[Source for registerOnChange](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L82-L84>)

**Modifiers:** inherited

Inherited from `SiIpInputDirective`

Implements [ControlValueAccessor](<https://angular.dev/api/forms/ControlValueAccessor>)

```
registerOnChange(fn: any): void
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [fn](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L82-L82>) | `any` |  | The callback function to register |

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)

##### registerOnTouched

[Source for registerOnTouched](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L86-L88>)

**Modifiers:** inherited

Inherited from `SiIpInputDirective`

Implements [ControlValueAccessor](<https://angular.dev/api/forms/ControlValueAccessor>)

```
registerOnTouched(fn: () => void): void
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [fn](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L86-L86>) | `() => void` |  | The callback function to register |

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)

##### setDisabledState

[Source for setDisabledState](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L90-L92>)

**Modifiers:** inherited

Inherited from `SiIpInputDirective`

Implements [ControlValueAccessor](<https://angular.dev/api/forms/ControlValueAccessor>)

```
setDisabledState(isDisabled: boolean): void
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [isDisabled](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L90-L90>) | `boolean` |  | The disabled status to set on the element |

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)

##### writeValue

[Source for writeValue](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L94-L97>)

**Modifiers:** inherited

Inherited from `SiIpInputDirective`

Implements [ControlValueAccessor](<https://angular.dev/api/forms/ControlValueAccessor>)

```
writeValue(value?: string | null): void
```

#### Parameters

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| [value](<https://github.com/siemens/element/blob/main/projects/element-ng/ip-input/si-ip-input.directive.ts#L94-L94>) (optional) | `string \| null` |  | The new value for the element |

**Returns:** [void](<https://www.typescriptlang.org/docs/handbook/2/functions.html#void>)
