ip validator

Validate an IP address
The validator supports both IPv4 and IPv6 addresses.

Options

Using with form field
The HTML attributes are used to set the validator options via the Declarative plugin
NameHTML attributeTypeDescription
ipv4data-fv-ip___ipv4BooleanEnable IPv4 validator, default to true
ipv6data-fv-ip___ipv6BooleanEnable IPv6 validator, default to true
messagedata-fv-ip___messageStringThe error message
Using with ES6 module
// You might need to change the importing path
import ip from 'formvalidation/dist/es6/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

It also supports CIDR notation.
ip validator

ES6 Module Example

The following snippet shows how to use the ip validator with ES6 module:
// You might need to change the importing path
import ip from 'formvalidation/dist/es6/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

See also