diff --git a/README.md b/README.md
index 8ef42fd79..8e0e8f013 100644
--- a/README.md
+++ b/README.md
@@ -153,7 +153,7 @@ Validator | Description
**isMobilePhone(str [, locale [, options]])** | check if the string is a mobile phone number,
`locale` is either an array of locales (e.g. `['sk-SK', 'sr-RS']`) OR one of `['am-Am', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-EH', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-PS', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'az-AZ', 'az-LB', 'az-LY', 'be-BY', 'bg-BG', 'bn-BD', 'bs-BA', 'ca-AD', 'cs-CZ', 'da-DK', 'de-AT', 'de-CH', 'de-DE', 'de-LU', 'dv-MV', 'dz-BT', 'el-CY', 'el-GR', 'en-AG', 'en-AI', 'en-AU', 'en-BM', 'en-BS', 'en-BW', 'en-CA', 'en-CM', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-IE', 'en-IN', 'en-JM', 'en-KE', 'en-KI', 'en-KN', 'en-LS', 'en-MO', 'en-MT', 'en-MU', 'en-MW', 'en-NG', 'en-NZ', 'en-PG', 'en-PH', 'en-PK', 'en-RW', 'en-SG', 'en-SL', 'en-SS', 'en-TZ', 'en-UG', 'en-US', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-EC', 'es-ES', 'es-GT', 'es-HN', 'es-MX', 'es-NI', 'es-PA', 'es-PE', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-AF', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF', 'fr-BJ', 'fr-CD', 'fr-CF', 'fr-DJ', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', 'fr-WF', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'ir-IR', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'ky-KG', 'lt-LT', 'mg-MG', 'mn-MN', 'mk-MK', 'ms-MY', 'my-MM', 'mz-MZ', 'nb-NO', 'ne-NP', 'nl-AW', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-AO', 'pt-BR', 'pt-PT', 'ro-Md', 'ro-RO', 'ru-RU', 'si-LK', 'sk-SK', 'sl-SI', 'so-SO', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tk-TM', 'tr-TR', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-MO', 'zh-TW']` OR defaults to `'any'`. If 'any' or a falsey value is used, function will check if any of the locales match).
`options` is an optional object that can be supplied with the following keys: `strictMode`, if this is set to `true`, the mobile phone number must be supplied with the country code and therefore must start with `+`. Locale list is `validator.isMobilePhoneLocales`.
**isMongoId(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
-**isNumeric(str [, options])** | check if the string contains only numbers.
`options` is an object which defaults to `{ no_symbols: false }` it also has `locale` as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).
`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fr-FR', 'fr-CA', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`.
+**isNumeric(str [, options])** | check if the string contains only numbers.
`options` is an object which defaults to `{no_symbols: false, thousand_separator: ''}` it also has `locale` as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`). `thousand_separator` can be set using a single character (e.g. `,` or `_`) to support validation of a number with a thousand separator.
`locale` determine the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`.
**isOctal(str)** | check if the string is a valid octal number.
**isPassportNumber(str, countryCode)** | check if the string is a valid passport number.
`countryCode` is one of `['AM', 'AR', 'AT', 'AU', 'AZ', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE', 'IN', 'IR', 'ID', 'IS', 'IT', 'JM', 'JP', 'KR', 'KZ', 'LI', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'NZ', 'PH', 'PK', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TH', 'TR', 'UA', 'US', 'ZA']`. Locale list is `validator.passportNumberLocales`.
**isPort(str)** | check if the string is a valid port number.
diff --git a/src/lib/isNumeric.js b/src/lib/isNumeric.js
index 4cc7ea5b3..e1fd01041 100644
--- a/src/lib/isNumeric.js
+++ b/src/lib/isNumeric.js
@@ -5,8 +5,21 @@ const numericNoSymbols = /^[0-9]+$/;
export default function isNumeric(str, options) {
assertString(str);
+
if (options && options.no_symbols) {
return numericNoSymbols.test(str);
}
- return (new RegExp(`^[+-]?([0-9]*[${(options || {}).locale ? decimal[options.locale] : '.'}])?[0-9]+$`)).test(str);
+
+ const decimal_char = (options || {}).locale ? decimal[options.locale] : '.';
+ if (options && options.thousands_separator) {
+ const separator = `${options.thousands_separator || ''}`;
+ if (separator.length > 1 || numericNoSymbols.test(separator)) {
+ throw new TypeError(`Expected non-numeric single character. Received thousand_separator: ${separator}`);
+ } else {
+ return new RegExp(`^[+-]?[0-9]{1,3}(${separator}[0-9]{3})*([${decimal_char}][0-9]+)?$`).test(str);
+ }
+ }
+
+ return (new RegExp(`^[+-]?([0-9]*[${decimal_char}])?[0-9]+$`)).test(str);
}
+
diff --git a/test/validators.test.js b/test/validators.test.js
index a4c3d7193..4590efc99 100644
--- a/test/validators.test.js
+++ b/test/validators.test.js
@@ -3186,26 +3186,97 @@ describe('Validators', () => {
});
});
- it('should validate numeric strings with locale', () => {
+ it('should validate numeric strings with thousand separator', () => {
test({
validator: 'isNumeric',
args: [{
- locale: 'fr-CA',
+ thousands_separator: '_',
}],
valid: [
'123',
- '00123',
- '-00123',
'0',
'-0',
'+123',
- '123,123',
- '+000000',
+ '123_123',
+ '123.000',
+ '123_123.000',
+ '123_123_123.000',
+ '+123_123_123.000',
+ '-123_123_123.000',
+ '+000_000',
],
invalid: [
' ',
'',
- '.',
+ ',',
+ '00123',
+ '-00123',
+ '_123',
+ '_123.000',
+ ],
+ });
+ });
+
+ it('should validate numeric strings with thousand separator and locale', () => {
+ test({
+ validator: 'isNumeric',
+ args: [{
+ thousands_separator: '_',
+ locale: 'fr-FR',
+ }],
+ valid: [
+ '123,000',
+ '123_123',
+ '123_123,000',
+ '123_123_123,000',
+ '+123_123_123,000',
+ '-123_123_123,000',
+ '+000_000,000',
+ ],
+ invalid: [
+ ' ',
+ '',
+ ',',
+ '_123',
+ '_123.000',
+ '1_23',
+ '1_23.000',
+ '1_22_123',
+ '1_22_123.000',
+ '123_123_123.000_123',
+ ],
+ });
+ });
+
+ it('should not validate numeric strings with invalid thousand_seperator chars', () => {
+ test({
+ validator: 'isNumeric',
+ args: [{
+ thousands_separator: '__',
+ }],
+ error: [
+ '123',
+ '123__123',
+ ],
+ });
+ test({
+ validator: 'isNumeric',
+ args: [{
+ thousands_separator: '9',
+ }],
+ error: [
+ '123',
+ '1239123',
+ ],
+ });
+ test({
+ validator: 'isNumeric',
+ args: [{
+ thousands_separator: '0',
+ }],
+ error: [
+ '123',
+ '1230123',
],
});
});