numeric validator
Check if the value is a numeric number
Options
Using with form field
Name | HTML attribute | Type | Description |
---|
message | data-fv-numeric___message | String | The error message |
thousandsSeparator | data-fv-numeric___thousands-separator | String | The thousands separator |
The popular values of the thousandsSeparator
option are:
- An empty string (the default value)
- A bank space
- A comma (
,
) - A dot (
.
)
Name | HTML attribute | Type | Description |
---|
decimalSeparator | data-fv-numeric___decimal-separator | String | The decimal separator |
The popular values of the decimalSeparator
option are:
- A dot (
.
) (the default value) - A comma (
,
)
The thousandsSeparator
and decimalSeparator
options are useful if your country use particular separators for thousands and decimal parts. See the next Supporting locales section for more details.
Using the ES6 module
import { numeric } from '/vendors/@form-validation/cjs/validator-numeric';
const result = numeric().validate({
value: ...,
options: {
decimalSeparator: ...,
message: ...,
thousandsSeparator: ...,
},
});
Using the npm package
- Install the validator package:
$ npm install @form-validation/validator-numeric
- Use the
numeric
validator:
import { numeric } from '@form-validation/validator-numeric';
const result = numeric().validate({
value: ...,
options: {
decimalSeparator: ...,
message: ...,
thousandsSeparator: ...,
},
});
Supporting locales
The thousands and decimal separators might take different value in certain countries. The following table introduces some popular values that are defined by various countries.
Country | Thousands separator | Decimal separator |
---|
United States | A comma (,) | A dot (.) |
France | A blank space | A comma (,) |
Italy | A dot (.) | A comma (,) |
formValidationInstance
.updateValidatorOption('number', 'numeric', 'thousandsSeparator', thousandsSeparator)
.updateValidatorOption('number', 'numeric', 'decimalSeparator', decimalSeparator);
Since the thousands and decimal separators are various, the field should use type="text"
attribute. Using type="number"
for field will restrict the input to use default separators for a number (an empty string for thousands, and a dot for decimal parts).
NPM package example
The following snippet shows how to use the numeric validator with the npm package:
import { numeric } from '@form-validation/validator-numeric';
const res1 = numeric().validate({
value: '967295.00',
options: {
thousandsSeparator: '',
decimalSeparator: '.',
message: 'The value is not a valid numeric number',
},
});
const res2 = numeric().validate({
value: '4967,295',
options: {
thousandsSeparator: ',',
decimalSeparator: '.',
message: 'The value is not a valid numeric number',
},
});
const res3 = numeric().validate({
value: '4 294 967 295',
options: {
thousandsSeparator: ' ',
decimalSeparator: ',',
message: 'The value is not a valid numeric number',
},
});
See also
Changelog
- The validator doesn't work properly if the
message
property isn't defined