lessThan validator

Check if the value is less than or equals to given number

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-less-than___inclusiveBooleanCan be true (default) or false.
If true, the input value must be less than or equal to the comparison one.
If false, the input value must be less than the comparison one.
max *data-fv-less-than___maxFloatThe number to make a comparison to
messagedata-fv-less-than___messageStringThe error message
Using the ES6 module
// You might need to change the importing path
import { lessThan } from '/vendors/@form-validation/cjs/validator-less-than';
const result = lessThan().validate({
value: ...,
options: {
inclusive: ...,
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-less-than
  • Use the lessThan validator:
import { lessThan } from '@form-validation/validator-less-than';
const result = lessThan().validate({
value: ...,
options: {
inclusive: ...,
max: ...,
message: ...,
},
});

NPM package example

The following snippet shows how to use the lessThan validator with the npm package:
import { lessThan } from '@form-validation/validator-less-than';
const res1 = lessThan().validate({
value: 20,
options: {
max: 30,
message: 'The value has to be less than 30',
},
});
// res1.valid === true
const res2 = lessThan().validate({
value: 30,
options: {
inclusive: false,
max: 30,
message: 'The value has to be less than 30',
},
});
// res2.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 option isn't passed to the placeholder message