between validator

Check if the input value is between (strictly or not) two given numbers

Options

Using with form field
The HTML attributes are used to set the validator options via the Declarative plugin
(* denotes a required option).
NameHTML attributeTypeDescription
inclusivedata-fv-between___inclusiveBooleanCan be true or false. If true, the input value must be in the range strictly
max*data-fv-between___max or maxFloatThe upper value in the range
messagedata-fv-between___messageStringThe error message
min*data-fv-between___min or minFloatThe lower value in the range
If you use min and max attributes, please set type="range".
Using the ES6 module
// You might need to change the importing path
import { between } from '/vendors/@form-validation/cjs/validator-between';
const result = between().validate({
value: ...,
options: {
inclusive: ...,
min: ...,
max: ...,
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-between
  • Use the between validator:
import { between } from '@form-validation/validator-between';
const result = between().validate({
value: ...,
options: {
inclusive: ...,
min: ...,
max: ...,
message: ...,
},
});

Basic example

The following example validates latitude and longitude values. A valid latitude must be between -90.0 and 90.0, and valid longitude may range from -180.0 to 180.0.
between validator

NPM package example

The following snippet shows how to use the between validator with the npm package:
import { between } from '@form-validation/validator-between';
const checkLatitude = between().validate({
value: 45,
options: {
inclusive: true,
min: -90,
max: 90,
message: 'The latitude must be between -90.0 and 90.0',
},
});
// checkLatitude.valid === true
const checkLongitude = between().validate({
value: 200,
options: {
inclusive: true,
min: -180,
max: 180,
message: 'The latitude must be between -180.0 and 180.0',
},
});
// checkLongitude.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
v1.6.0
  • Fixed an issue that the max and min options aren't passed to the placeholder message