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 the ES6 module
// You might need to change the importing path
import { ip } from '/vendors/@form-validation/cjs/validator-ip';
const result = ip().validate({
value: ...,
options: {
ipv4: ...,
ipv6: ...,
message: ...,
},
});
/*
result is an object of
{
valid: true or false,
message: The error message
}
*/
Using the npm package
  • Install the validator package:
$ npm install @form-validation/validator-ip
  • Use the ip validator:
import { ip } from '@form-validation/validator-ip';
const result = ip().validate({
value: ...,
options: {
ipv4: ...,
ipv6: ...,
message: ...,
},
});

Basic example

It also supports CIDR notation.
ip validator

NPM package example

The following snippet shows how to use the ip validator with the npm package:
import { ip } from '@form-validation/validator-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

Changelog

v2.1.0
  • The validator doesn't work properly if the message property isn't defined
v2.0.0
  • Add the npm package