Utils / Full Stack / isValidIP
Function: isValidIP()
ts
function isValidIP(ip, options?): boolean;Validates if the given string is a valid IP address.
Supports IPv4, IPv6, or both depending on the version option.
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| The string to validate as an IP address. |
| { | Validation options. |
|
| If specified, restricts validation to |
Returns
boolean
true if the string is a valid IP address, otherwise false.
Example
typescript
isValidIP("192.168.1.1"); // true
isValidIP("192.168.1.1", { version: 4 }); // true
isValidIP("192.168.1.1", { version: 6 }); // false
isValidIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); // true
isValidIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334", { version: 6 }); // true
isValidIP("localhost"); // false