Documentation v4.0.0

Overview

Check if the string is an IP address (version 4 or 6).

Import

import {validators} from 'metronic-extension';
const {isIP} = validators;

Signature

isIP(value: string, options?: IsIPOptions): boolean
Parameter Type Default Description
value string - Value to be validated.
options.ipVersion '4' | '6' | 4 | 6 undefined IP version to validate. If omitted, allows both IPv4 and IPv6.
options.allowIPRange boolean false If true, allow IP range input (e.g. 127.0.0.1/24, 2001::/128).
Returns: booleantrue if valid, false otherwise.

Example

Enter an IP address to validate in real time.
Result: true
<div class="mb-3">
  <label class="form-label">Input value</label>
  <input id="isIPInput" class="form-control form-control-solid" placeholder="Enter an IP address..." value="192.168.1.1">
</div>
<div>
  <span class="fw-bold">Result: </span>
  <span id="isIPResult" class="badge badge-success">true</span>
</div>
import {validators} from 'metronic-extension';
const {isIP} = validators;

// Any IP version
isIP('127.0.0.1');                                  // true
isIP('::1');                                        // true

// Specific version
isIP('1.2.3.4', {ipVersion: 4});                    // true
isIP('fe80::a6db:30ff:fe98:e946', {ipVersion: 6});  // true

// IP ranges
isIP('127.0.0.1/24', {allowIPRange: true});         // true
isIP('2001::/128', {allowIPRange: true});            // true