ip validator

Validate an IP address. Support both IPv4 and IPv6

Options

Using with form field

The HTML attributes are used to set the validator options via the Declarative plugin

Name HTML attribute Type Description
ipv4 data-fv-ip___ipv4 Boolean Enable IPv4 validator, default to true
ipv6 data-fv-ip___ipv6 Boolean Enable IPv6 validator, default to true
message data-fv-ip___message String The error message

Using with JavaScript module


// You might need to change the importing path
import ip from 'formvalidation/validators/ip';

const result = ip().validate({
    value: ...,
    options: {
        ipv4: ...,
        ipv6: ...,
        message: ...,
    },
});
/*
result is an object of
{
    valid: true or false,
    message: The error message
}
*/

Basic Example

You can click on sample to see if it's a valid or invalid IP address.

Sample Is valid?
0.0.0.0
192.168.1.1
255.255.255.255
0000:0000:0000:0000:0000:0000:0000:0000
fe00::1
fe80::217:f2ff:fe07:ed62
ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
2001:0db8:0000:85a3:0000:0000:ac1f:8001
10.168.0001.100
0.0.0.256
256.255.255.255
256.0.0.0
192.168. 224.0
192.168.224.0 1
02001:0000:1234:0000:0000:C1C0:ABCD:0876
2001:0000:1234:0000:00001:C1C0:ABCD:0876
2001:0000:1234: 0000:0000:C1C0:ABCD:0876
2001:0000:1234:0000:0000:C1C0:ABCD:0876 0
3ffe:0b00:0000:0001:0000:0000:000a
FF02:0000:0000:0000:0000:0000:0000:0000:0001
::1111:2222:3333:4444:5555:6666::
3ffe:b00::1::a

It also supports CIDR notation:

Sample Is valid?
192.168.1.1/12
192.168.1.1/32
0000:0000:0000:0000:0000:0000:0000:0000/01
0000:0000:0000:0000:0000:0000:0000:0000/19
0000:0000:0000:0000:0000:0000:0000:0000/99
0000:0000:0000:0000:0000:0000:0000:0000/100
0000:0000:0000:0000:0000:0000:0000:0000/119
0000:0000:0000:0000:0000:0000:0000:0000/128
::1/128
2001:db8::/48
192.168.1.1/
192.168.1.1/12.34
192.168.1.1/01
192.168.1.1/33
0000:0000:0000:0000:0000:0000:0000:0000/
0000:0000:0000:0000:0000:0000:0000:0000/012
0000:0000:0000:0000:0000:0000:0000:0000/129
0000:0000:0000:0000:0000:0000:0000:0000/130

Module Example

The following snippet shows how to use the ip validator with JavaScript module:


// You might need to change the importing path
import ip from 'formvalidation/validators/ip';

const res1 = ip().validate({
    value: '1FAHP26W49G252740',
    options: {
        ipv6: false,
        message: 'The value is not valid IP v4',
    },
});
// res1.valid === true

const res2 = ip().validate({
    value: 'fe80::217:f2ff:fe07:ed62',
    options: {
        ipv4: false,
        message: 'The value is not valid IP v6',
    },
});
// res2.valid === true

const res3 = ip().validate({
    value: '192.168.1.1/01',
    options: {
        ipv4: false,
        message: 'The value is not valid IP',
    },
});
// res3.valid === false

Related validators

The following validators are similar to this one and might be useful to you.

Name Description
mac Validate a MAC address
uri Validate an URL address