ip validator
Validate an IP address
The validator supports both IPv4 and IPv6 addresses.
Options
Using with form field
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 the ES6 module
import { ip } from '/vendors/@form-validation/cjs/validator-ip';
const result = ip().validate({
value: ...,
options: {
ipv4: ...,
ipv6: ...,
message: ...,
},
});
Using the npm package
- Install the validator package:
$ npm install @form-validation/validator-ip
import { ip } from '@form-validation/validator-ip';
const result = ip().validate({
value: ...,
options: {
ipv4: ...,
ipv6: ...,
message: ...,
},
});
Basic example
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',
},
});
const res2 = ip().validate({
value: 'fe80::217:f2ff:fe07:ed62',
options: {
ipv4: false,
message: 'The value is not valid IP v6',
},
});
const res3 = ip().validate({
value: '192.168.1.1/01',
options: {
ipv4: false,
message: 'The value is not valid IP',
},
});
See also
Changelog
- The validator doesn't work properly if the
message
property isn't defined