integer validator

Validate an integer number. Accept both positive and negative number

Options

Use with form field

The HTML attributes are used to set the validator options via the Declarative plugin

Name HTML attribute Type Description
message data-fv-integer___message String The error message
thousandsSeparator data-fv-integer___thousands-separator String The thousands separator. The popular values are
  • An empty string (the default value)
  • A bank space
  • A comma (,)
  • A dot (.)
decimalSeparator data-fv-integer___decimal-separator String The decimal separator. The popular values 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.

Use with JavaScript module


// You might need to change the importing path
import integer from 'formvalidation/validators/integer';

const result = integer().validate({
    value: ...,
    options: {
        decimalSeparator: ...,
        message: ...,
        thousandsSeparator: ...,
    },
});
/*
result is an object of
{
    valid: true or false,
    message: The error message
}
*/

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.

You can click the sample number to test:

Country Thousands separator Decimal separator Sample Is valid?
An empty string A dot (.) 95
095
0095
295
7295
67295
967295.00
4294967295.000
7,295
67,295
967,295
4967,295
67295.01
4,294,967,295,00
4294967295.001
United States A comma (,) A dot (.) 95
295
7,295
67,295
967,295
967,295.00
4,294,967,295
4,294,967,295.00
4,294,967,295.000
7295
67295.01
4967,295
4294,967,295.0001
4294,967,295.00
4,294,967,295,00
42,94,967,295.00
42,94,967295.00
France A blank space A comma (,) 95
295
67 295
967 295
967 295,00
4 294 967 295
4 294 967 295,00
4 294 967 295,000
7295
67295,01
4967 295
4 294 967 295,0001
4294 967 295,00
4 294 967 295.00
42 94 967 295,00
42 94 967295,00
Italy A dot (.) A comma (,) 95
295
7.295
67.295
967.295
967.295,00
4.294.967.295
4.294.967.295,00
4.294.967.295,000
7295
67295,01
4967.295
967.295.00
4294.967.295,00
4.294.967.295.00
42.94.967.295,00
42.94.967295,00

The example also uses the updateValidatorOption() method to set values for thousandsSeparator and decimalSeparator options.


formValidationInstance
    // Update the options
    .updateValidatorOption('number', 'integer', 'thousandsSeparator', thousandsSeparator)
    .updateValidatorOption('number', 'integer', 'decimalSeparator', decimalSeparator);

Tip

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 an integer number (an empty string for thousands, and a dot for decimal parts).

Basic example

HTML5 Example

When the Declarative plugin is used, the integer validator will be enabled automatically when using HTML5 type="number" attribute.

Module example

The following snippet shows how to use the integer validator with JavaScript module:


// You might need to change the importing path
import integer from 'formvalidation/validators/integer';

const res1 = integer().validate({
    value: '967295.00',
    options: {
        thousandsSeparator: '',
        decimalSeparator: '.',
        message: 'The value is not a valid integer number',
    },
});
// res1.valid === true

const res2 = integer().validate({
    value: '4967,295',
    options: {
        thousandsSeparator: ',',
        decimalSeparator: '.',
        message: 'The value is not a valid integer number',
    },
});
// res2.valid === false

const res3 = integer().validate({
    value: '4 294 967 295',
    options: {
        thousandsSeparator: ' ',
        decimalSeparator: ',',
        message: 'The value is not a valid integer number',
    },
});
// res3.valid === true

Related validators

The following validators are similar to this one and might be useful to you.

Name Description
digits Check if the value contains only digits
numeric Check if the value is numeric