What is is-ip?
The is-ip npm package is a utility library for checking if a given string is a valid IP address. It supports both IPv4 and IPv6 addresses and provides simple, easy-to-use functions for validation.
What are is-ip's main functionalities?
Check if a string is a valid IP address
This feature allows you to check if a given string is a valid IP address, whether it's IPv4 or IPv6.
const isIp = require('is-ip');
console.log(isIp('192.168.0.1')); // true
console.log(isIp('::1')); // true
console.log(isIp('invalid-ip')); // false
Check if a string is a valid IPv4 address
This feature allows you to specifically check if a given string is a valid IPv4 address.
const isIp = require('is-ip');
console.log(isIp.v4('192.168.0.1')); // true
console.log(isIp.v4('::1')); // false
Check if a string is a valid IPv6 address
This feature allows you to specifically check if a given string is a valid IPv6 address.
const isIp = require('is-ip');
console.log(isIp.v6('::1')); // true
console.log(isIp.v6('192.168.0.1')); // false
Other packages similar to is-ip
ip
The 'ip' package provides a comprehensive set of utilities for IP address manipulation, including validation, conversion, and subnet calculations. It offers more advanced features compared to is-ip, such as converting IP addresses to different formats and performing subnet calculations.
net
The 'net' package is a built-in Node.js module that provides utilities for network-related operations, including IP address validation. While it is not as specialized as is-ip, it offers a broader range of network functionalities.
ipaddr.js
The 'ipaddr.js' package is a library for manipulating IPv4 and IPv6 addresses in JavaScript. It provides validation, parsing, and comparison functionalities. It is more feature-rich compared to is-ip, offering more detailed manipulation and analysis of IP addresses.
is-ip
Check if a string is an IP address
If you only need this for Node.js and don't care about browser support, you may want to use net.isIP
instead. Note that it returns an integer instead of a boolean.
Install
npm install is-ip
Usage
import {isIP, isIPv4} from 'is-ip';
isIP('1:2:3:4:5:6:7:8');
isIP('192.168.0.1');
isIPv4('1:2:3:4:5:6:7:8');
API
isIP(string)
Check if string
is IPv6 or IPv4.
isIPv6(string)
Check if string
is IPv6.
isIPv4(string)
Check if string
is IPv4.
ipVersion(string)
Returns 6
if string
is IPv6, 4
if string
is IPv4, or undefined
if string
is neither.
import {ipVersion} from 'is-ip';
ipVersion('1:2:3:4:5:6:7:8');
ipVersion('192.168.0.1');
ipVersion('abc');
Related
- ip-regex - Regular expression for matching IP addresses
- is-cidr - Check if a string is an IP address in CIDR notation
- cidr-regex - Regular expression for matching IP addresses in CIDR notation