From 5e711b617cefd6a84fcfefd1ce5773e8aa17a2cb Mon Sep 17 00:00:00 2001 From: Songyue Wang Date: Wed, 11 Jan 2023 15:30:17 +0800 Subject: [PATCH 1/6] Added isContainerId --- README.md | 1 + src/index.js | 2 ++ src/lib/isContainerID.js | 28 ++++++++++++++++++++++++++++ test/validators.js | 24 ++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 src/lib/isContainerID.js diff --git a/README.md b/README.md index 9372230e7..49c2bf0d0 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ Validator | Description **isBoolean(str [, options])** | check if a string is a boolean.
`options` is an object which defaults to `{ loose: false }`. If loose is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If loose is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (eg: ['true', 'True', 'TRUE']). **isBtcAddress(str)** | check if the string is a valid BTC address. **isByteLength(str [, options])** | check if the string's length (in UTF-8 bytes) falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. +**isContainerID(str)** | check if the string is an ISO6346 shipping container identification. **isCreditCard(card, [, options])** | check if the string is a credit card.

options is an optional object that can be supplied with the following key(s): `provider` is an optional key whose value should be a string, and defines the company issuing the credit card. Valid values include `amex` , `dinersclub` , `discover` , `jcb` , `mastercard` , `unionpay` , `visa` or blank will check for any provider. **isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. **isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs). diff --git a/src/index.js b/src/index.js index 42e1e8b69..2f2e460d5 100644 --- a/src/index.js +++ b/src/index.js @@ -78,6 +78,7 @@ import isISIN from './lib/isISIN'; import isISBN from './lib/isISBN'; import isISSN from './lib/isISSN'; import isTaxID from './lib/isTaxID'; +import isContainerID from './lib/isContainerID'; import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone'; @@ -195,6 +196,7 @@ const validator = { isMobilePhoneLocales, isPostalCode, isPostalCodeLocales, + isContainerID, isEthereumAddress, isCurrency, isBtcAddress, diff --git a/src/lib/isContainerID.js b/src/lib/isContainerID.js new file mode 100644 index 000000000..98a7ac7a6 --- /dev/null +++ b/src/lib/isContainerID.js @@ -0,0 +1,28 @@ +import assertString from './util/assertString'; + +// https://en.wikipedia.org/wiki/ISO_6346 +const isContainerIdReg = new RegExp('^[A-Z]{3}U[0-9]{7}$'); +const isDigit = new RegExp('^[0-9]{1}'); + +export default function isContainerID(str) { + assertString(str); + str = str.trim(); + + if (!isContainerIdReg.test(str)) return false; + + let sum = 0; + for (let i = 0; i < str.length - 1; i++) { + if (!isDigit.test(str[i])) { + let convertedCode; + const letterCode = str.charCodeAt(i) - 55; + if (letterCode < 11) convertedCode = letterCode; + else if (letterCode >= 11 && letterCode <= 20) convertedCode = 12 + (letterCode % 11); + else if (letterCode >= 21 && letterCode <= 30) convertedCode = 23 + (letterCode % 21); + else convertedCode = 34 + (letterCode % 31); + sum += convertedCode * (2 ** i); + } else sum += str[i] * (2 ** i); + } + + const checkSumDigit = sum % 11; + return Number(str[str.length - 1]) === checkSumDigit; +} diff --git a/test/validators.js b/test/validators.js index c0e36bec7..485289b6c 100644 --- a/test/validators.js +++ b/test/validators.js @@ -11837,6 +11837,30 @@ describe('Validators', () => { }); }); + it('should validate containerID', () => { + test({ + validator: 'isContainerID', + valid: [ + 'HLXU2008419', + 'TGHU7599330', + 'ECMU4657496', + 'MEDU6246078', + 'YMLU2809976', + 'MRKU0046221', + 'EMCU3811879', + 'OOLU8643084', + 'HJCU1922713', + ], + invalid: [ + 'OOLU1922713', + 'HJCU1922413', + 'FCUI985619', + 'ECMJ4657496', + 'TBJA7176445', + ], + }); + }); + // EU-UK valid numbers sourced from https://ec.europa.eu/taxation_customs/tin/specs/FS-TIN%20Algorithms-Public.docx or constructed by @tplessas. it('should validate taxID', () => { test({ From 638c89b4014418df25c47c8973d2ad0b512521c3 Mon Sep 17 00:00:00 2001 From: Songyue Wang Date: Wed, 11 Jan 2023 15:38:01 +0800 Subject: [PATCH 2/6] New test added --- test/validators.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/validators.js b/test/validators.js index 485289b6c..1d02c476f 100644 --- a/test/validators.js +++ b/test/validators.js @@ -11857,6 +11857,7 @@ describe('Validators', () => { 'FCUI985619', 'ECMJ4657496', 'TBJA7176445', + 'AFFU5962593', ], }); }); From a81dfe1d0d12091bd749c4b162bfecff93f5826e Mon Sep 17 00:00:00 2001 From: Songyue Wang Date: Thu, 12 Jan 2023 21:12:46 +0800 Subject: [PATCH 3/6] Updated RegEx usage and validator name --- README.md | 2 +- src/index.js | 4 ++-- src/lib/isContainerID.js | 28 ---------------------------- src/lib/isISO6346.js | 35 +++++++++++++++++++++++++++++++++++ test/validators.js | 5 +++-- 5 files changed, 41 insertions(+), 33 deletions(-) delete mode 100644 src/lib/isContainerID.js create mode 100644 src/lib/isISO6346.js diff --git a/README.md b/README.md index 49c2bf0d0..d56a3433c 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,6 @@ Validator | Description **isBoolean(str [, options])** | check if a string is a boolean.
`options` is an object which defaults to `{ loose: false }`. If loose is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If loose is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (eg: ['true', 'True', 'TRUE']). **isBtcAddress(str)** | check if the string is a valid BTC address. **isByteLength(str [, options])** | check if the string's length (in UTF-8 bytes) falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. -**isContainerID(str)** | check if the string is an ISO6346 shipping container identification. **isCreditCard(card, [, options])** | check if the string is a credit card.

options is an optional object that can be supplied with the following key(s): `provider` is an optional key whose value should be a string, and defines the company issuing the credit card. Valid values include `amex` , `dinersclub` , `discover` , `jcb` , `mastercard` , `unionpay` , `visa` or blank will check for any provider. **isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. **isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs). @@ -130,6 +129,7 @@ Validator | Description **isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6). **isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13). **isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier). +**isISO6346(str)** | check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. **isISO6391(str)** | check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code. **isISO8601(str [, options])** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid. **isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code. diff --git a/src/index.js b/src/index.js index 2f2e460d5..bebf42239 100644 --- a/src/index.js +++ b/src/index.js @@ -78,7 +78,6 @@ import isISIN from './lib/isISIN'; import isISBN from './lib/isISBN'; import isISSN from './lib/isISSN'; import isTaxID from './lib/isTaxID'; -import isContainerID from './lib/isContainerID'; import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone'; @@ -88,6 +87,7 @@ import isCurrency from './lib/isCurrency'; import isBtcAddress from './lib/isBtcAddress'; +import isISO6346 from './lib/isISO6346'; import isISO6391 from './lib/isISO6391'; import isISO8601 from './lib/isISO8601'; import isRFC3339 from './lib/isRFC3339'; @@ -196,10 +196,10 @@ const validator = { isMobilePhoneLocales, isPostalCode, isPostalCodeLocales, - isContainerID, isEthereumAddress, isCurrency, isBtcAddress, + isISO6346, isISO6391, isISO8601, isRFC3339, diff --git a/src/lib/isContainerID.js b/src/lib/isContainerID.js deleted file mode 100644 index 98a7ac7a6..000000000 --- a/src/lib/isContainerID.js +++ /dev/null @@ -1,28 +0,0 @@ -import assertString from './util/assertString'; - -// https://en.wikipedia.org/wiki/ISO_6346 -const isContainerIdReg = new RegExp('^[A-Z]{3}U[0-9]{7}$'); -const isDigit = new RegExp('^[0-9]{1}'); - -export default function isContainerID(str) { - assertString(str); - str = str.trim(); - - if (!isContainerIdReg.test(str)) return false; - - let sum = 0; - for (let i = 0; i < str.length - 1; i++) { - if (!isDigit.test(str[i])) { - let convertedCode; - const letterCode = str.charCodeAt(i) - 55; - if (letterCode < 11) convertedCode = letterCode; - else if (letterCode >= 11 && letterCode <= 20) convertedCode = 12 + (letterCode % 11); - else if (letterCode >= 21 && letterCode <= 30) convertedCode = 23 + (letterCode % 21); - else convertedCode = 34 + (letterCode % 31); - sum += convertedCode * (2 ** i); - } else sum += str[i] * (2 ** i); - } - - const checkSumDigit = sum % 11; - return Number(str[str.length - 1]) === checkSumDigit; -} diff --git a/src/lib/isISO6346.js b/src/lib/isISO6346.js new file mode 100644 index 000000000..fe1dd2d55 --- /dev/null +++ b/src/lib/isISO6346.js @@ -0,0 +1,35 @@ +import assertString from './util/assertString'; + +// https://en.wikipedia.org/wiki/ISO_6346 +// according to ISO6346 standard, checksum digit is mandatory for freight container but recommended +// for other container types (J and Z) +const isISO6346Str = /^[A-Z]{3}(U[0-9]{7})|([J,Z][0-9]{6,7})$/; +const isDigit = /^[0-9]$/; + +export default function isISO6346(str) { + assertString(str); + str = str.trim(); + str = str.toUpperCase(); + + if (!isISO6346Str.test(str)) return false; + + if (str.length === 11) { + let sum = 0; + for (let i = 0; i < str.length - 1; i++) { + if (!isDigit.test(str[i])) { + let convertedCode; + const letterCode = str.charCodeAt(i) - 55; + if (letterCode < 11) convertedCode = letterCode; + else if (letterCode >= 11 && letterCode <= 20) convertedCode = 12 + (letterCode % 11); + else if (letterCode >= 21 && letterCode <= 30) convertedCode = 23 + (letterCode % 21); + else convertedCode = 34 + (letterCode % 31); + sum += convertedCode * (2 ** i); + } else sum += str[i] * (2 ** i); + } + + const checkSumDigit = sum % 11; + return Number(str[str.length - 1]) === checkSumDigit; + } + + return true; +} diff --git a/test/validators.js b/test/validators.js index 1d02c476f..c9069a6e0 100644 --- a/test/validators.js +++ b/test/validators.js @@ -11837,9 +11837,9 @@ describe('Validators', () => { }); }); - it('should validate containerID', () => { + it('should validate ISO6346 shipping containerID', () => { test({ - validator: 'isContainerID', + validator: 'isISO6346', valid: [ 'HLXU2008419', 'TGHU7599330', @@ -11850,6 +11850,7 @@ describe('Validators', () => { 'EMCU3811879', 'OOLU8643084', 'HJCU1922713', + 'QJRZ123456', ], invalid: [ 'OOLU1922713', From a48637c6678ce37b000888cc8552acc3b2ababdc Mon Sep 17 00:00:00 2001 From: Songyue Wang Date: Mon, 23 Jan 2023 21:39:09 +0800 Subject: [PATCH 4/6] Added alias isFreightContainerID and removed sanitization --- README.md | 1 + src/index.js | 3 ++- src/lib/isISO6346.js | 6 ++++-- test/validators.js | 25 +++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d56a3433c..35702c34c 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ Validator | Description **isEthereumAddress(str)** | check if the string is an [Ethereum](https://ethereum.org/) address using basic regex. Does not validate address checksums. **isFloat(str [, options])** | check if the string is a float.

`options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`) it also has `locale` as an option.

`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.

`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-CA', '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']`. Locale list is `validator.isFloatLocales`. **isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).

`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false }`. If `allow_wildcard` is set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`). +**isFreightContainerID(str)** | alias for `isISO6346`, check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. **isFullWidth(str)** | check if the string contains any full-width chars. **isHalfWidth(str)** | check if the string contains any half-width chars. **isHash(str, algorithm)** | check if the string is a hash of type algorithm.

Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']` diff --git a/src/index.js b/src/index.js index bebf42239..e6ca22bbe 100644 --- a/src/index.js +++ b/src/index.js @@ -87,7 +87,7 @@ import isCurrency from './lib/isCurrency'; import isBtcAddress from './lib/isBtcAddress'; -import isISO6346 from './lib/isISO6346'; +import { isISO6346, isFreightContainerID } from './lib/isISO6346'; import isISO6391 from './lib/isISO6391'; import isISO8601 from './lib/isISO8601'; import isRFC3339 from './lib/isRFC3339'; @@ -200,6 +200,7 @@ const validator = { isCurrency, isBtcAddress, isISO6346, + isFreightContainerID, isISO6391, isISO8601, isRFC3339, diff --git a/src/lib/isISO6346.js b/src/lib/isISO6346.js index fe1dd2d55..0cb657e7c 100644 --- a/src/lib/isISO6346.js +++ b/src/lib/isISO6346.js @@ -6,9 +6,9 @@ import assertString from './util/assertString'; const isISO6346Str = /^[A-Z]{3}(U[0-9]{7})|([J,Z][0-9]{6,7})$/; const isDigit = /^[0-9]$/; -export default function isISO6346(str) { +export function isISO6346(str) { assertString(str); - str = str.trim(); + str = str.toUpperCase(); if (!isISO6346Str.test(str)) return false; @@ -33,3 +33,5 @@ export default function isISO6346(str) { return true; } + +export const isFreightContainerID = isISO6346; diff --git a/test/validators.js b/test/validators.js index c9069a6e0..4aecd1403 100644 --- a/test/validators.js +++ b/test/validators.js @@ -11862,6 +11862,31 @@ describe('Validators', () => { ], }); }); + it('should validate ISO6346 shipping containerID', () => { + test({ + validator: 'isFreightContainerID', + valid: [ + 'HLXU2008419', + 'TGHU7599330', + 'ECMU4657496', + 'MEDU6246078', + 'YMLU2809976', + 'MRKU0046221', + 'EMCU3811879', + 'OOLU8643084', + 'HJCU1922713', + 'QJRZ123456', + ], + invalid: [ + 'OOLU1922713', + 'HJCU1922413', + 'FCUI985619', + 'ECMJ4657496', + 'TBJA7176445', + 'AFFU5962593', + ], + }); + }); // EU-UK valid numbers sourced from https://ec.europa.eu/taxation_customs/tin/specs/FS-TIN%20Algorithms-Public.docx or constructed by @tplessas. it('should validate taxID', () => { From b76305e78a8b17dc593656e9bd7a12b4ec0b286a Mon Sep 17 00:00:00 2001 From: Songyue Wang Date: Mon, 6 Feb 2023 12:05:29 +0800 Subject: [PATCH 5/6] Fixed M/C --- README.md | 139 +++-- test/{client-side.js => clientSide.test.js} | 0 test/{exports.js => exports.test.js} | 0 test/{sanitizers.js => sanitizers.test.js} | 0 test/testFunctions.js | 56 ++ test/{util.js => util.test.js} | 0 test/{validators.js => validators.test.js} | 655 ++++++++++++++------ test/validators/isAfter.test.js | 61 ++ test/validators/isISBN.test.js | 109 ++++ 9 files changed, 787 insertions(+), 233 deletions(-) rename test/{client-side.js => clientSide.test.js} (100%) rename test/{exports.js => exports.test.js} (100%) rename test/{sanitizers.js => sanitizers.test.js} (100%) create mode 100644 test/testFunctions.js rename test/{util.js => util.test.js} (100%) rename test/{validators.js => validators.test.js} (96%) create mode 100644 test/validators/isAfter.test.js create mode 100644 test/validators/isISBN.test.js diff --git a/README.md b/README.md index 35702c34c..e27459f62 100644 --- a/README.md +++ b/README.md @@ -88,90 +88,91 @@ Here is a list of the validators currently available. Validator | Description --------------------------------------- | -------------------------------------- -**contains(str, seed [, options ])** | check if the string contains the seed.

`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.
Options:
`ignoreCase`: Ignore case when doing comparison, default false
`minOccurences`: Minimum number of occurrences for the seed in the string. Defaults to 1. +**contains(str, seed [, options])** | check if the string contains the seed.

`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.
Options:
`ignoreCase`: Ignore case when doing comparison, default false.
`minOccurences`: Minimum number of occurrences for the seed in the string. Defaults to 1. **equals(str, comparison)** | check if the string matches the comparison. -**isAfter(str [, date])** | check if the string is a date that's after the specified date (defaults to now). -**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).

Locale 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', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. -**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).

Locale 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', 'bn', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. +**isAfter(str [, options])** | check if the string is a date that is after the specified date.

`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.
**Options:**
`comparisonDate`: Date to compare to. Defaults to `Date().toString()` (now). +**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).

`locale` 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', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']` and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. +**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).

`locale` 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', 'bn', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s. **isAscii(str)** | check if the string contains ASCII chars only. -**isBase32(str [, options])** | check if a string is base32 encoded. `options` is optional and defaults to `{crockford: false}`.
When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative](http://www.crockford.com/base32.html). -**isBase58(str)** | check if a string is base58 encoded. -**isBase64(str [, options])** | check if a string is base64 encoded. options is optional and defaults to `{urlSafe: false}`
when `urlSafe` is true it tests the given base64 encoded string is [url safe](https://base64.guru/standards/base64url) -**isBefore(str [, date])** | check if the string is a date that's before the specified date. -**isBIC(str)** | check if a string is a BIC (Bank Identification Code) or SWIFT code. -**isBoolean(str [, options])** | check if a string is a boolean.
`options` is an object which defaults to `{ loose: false }`. If loose is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If loose is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (eg: ['true', 'True', 'TRUE']). +**isBase32(str [, options])** | check if the string is base32 encoded. `options` is optional and defaults to `{ crockford: false }`.
When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative][Crockford Base32]. +**isBase58(str)** | check if the string is base58 encoded. +**isBase64(str [, options])** | check if the string is base64 encoded. `options` is optional and defaults to `{ urlSafe: false }`
when `urlSafe` is true it tests the given base64 encoded string is [url safe][Base64 URL Safe]. +**isBefore(str [, date])** | check if the string is a date that is before the specified date. +**isBIC(str)** | check if the string is a BIC (Bank Identification Code) or SWIFT code. +**isBoolean(str [, options])** | check if the string is a boolean.
`options` is an object which defaults to `{ loose: false }`. If `loose` is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If `loose` is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (e.g.: ['true', 'True', 'TRUE']). **isBtcAddress(str)** | check if the string is a valid BTC address. -**isByteLength(str [, options])** | check if the string's length (in UTF-8 bytes) falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. -**isCreditCard(card, [, options])** | check if the string is a credit card.

options is an optional object that can be supplied with the following key(s): `provider` is an optional key whose value should be a string, and defines the company issuing the credit card. Valid values include `amex` , `dinersclub` , `discover` , `jcb` , `mastercard` , `unionpay` , `visa` or blank will check for any provider. -**isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. -**isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs). -**isDate(input [, options])** | Check if the input is a valid date. e.g. [`2002-07-15`, new Date()].

`options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`

`format` is a string and defaults to `YYYY/MM/DD`.

`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject inputs different from `format`.

`delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`. -**isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`

`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', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.
**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'. -**isDivisibleBy(str, number)** | check if the string is a number that's divisible by another. -**isEAN(str)** | check if the string is an EAN (European Article Number). -**isEmail(str [, options])** | check if the string is an email.

`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name `. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name `. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, e-mail addresses without having TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by GMail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails. -**isEmpty(str [, options])** | check if the string has a length of zero.

`options` is an object which defaults to `{ ignore_whitespace:false }`. -**isEthereumAddress(str)** | check if the string is an [Ethereum](https://ethereum.org/) address using basic regex. Does not validate address checksums. -**isFloat(str [, options])** | check if the string is a float.

`options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`) it also has `locale` as an option.

`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.

`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-CA', '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']`. Locale list is `validator.isFloatLocales`. -**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).

`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false }`. If `allow_wildcard` is set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`). +**isByteLength(str [, options])** | check if the string's length (in UTF-8 bytes) falls in a range.

`options` is an object which defaults to `{ min: 0, max: undefined }`. +**isCreditCard(str [, options])** | check if the string is a credit card number.

`options` is an optional object that can be supplied with the following key(s): `provider` is an optional key whose value should be a string, and defines the company issuing the credit card. Valid values include `['amex', 'dinersclub', 'discover', 'jcb', 'mastercard', 'unionpay', 'visa']` or blank will check for any provider. +**isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{ symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false }`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. +**isDataURI(str)** | check if the string is a [data uri format][Data URI Format]. +**isDate(str [, options])** | check if the string is a valid date. e.g. [`2002-07-15`, new Date()].

`options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`.

`format` is a string and defaults to `YYYY/MM/DD`.

`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject strings different from `format`.

`delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`. +**isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`.

`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', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.
**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'. +**isDivisibleBy(str, number)** | check if the string is a number that is divisible by another. +**isEAN(str)** | check if the string is an [EAN (European Article Number)][European Article Number]. +**isEmail(str [, options])** | check if the string is an email.

`options` is an object which defaults to `{ allow_display_name: false, require_display_name: false, allow_utf8_local_part: true, require_tld: true, allow_ip_domain: false, domain_specific_validation: false, blacklisted_chars: '', host_blacklist: [] }`. If `allow_display_name` is set to true, the validator will also match `Display Name `. If `require_display_name` is set to true, the validator will reject strings without the format `Display Name `. If `allow_utf8_local_part` is set to false, the validator will not allow any non-English UTF8 character in email address' local part. If `require_tld` is set to false, email addresses without a TLD in their domain will also be matched. If `ignore_max_length` is set to true, the validator will not check for the standard max length of an email. If `allow_ip_domain` is set to true, the validator will allow IP addresses in the host part. If `domain_specific_validation` is true, some additional validation will be enabled, e.g. disallowing certain syntactically valid email addresses that are rejected by Gmail. If `blacklisted_chars` receives a string, then the validator will reject emails that include any of the characters in the string, in the name part. If `host_blacklist` is set to an array of strings and the part of the email after the `@` symbol matches one of the strings defined in it, the validation fails. If `host_whitelist` is set to an array of strings and the part of the email after the `@` symbol matches none of the strings defined in it, the validation fails. +**isEmpty(str [, options])** | check if the string has a length of zero.

`options` is an object which defaults to `{ ignore_whitespace: false }`. +**isEthereumAddress(str)** | check if the string is an [Ethereum][Ethereum] address. Does not validate address checksums. +**isFloat(str [, options])** | check if the string is a float.

`options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`) it also has `locale` as an option.

`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.

`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', 'es-ES', 'fr-CA', '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']`. Locale list is `validator.isFloatLocales`. +**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).

`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, ignore_max_length: false }`. If `allow_wildcard` is set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`). **isFreightContainerID(str)** | alias for `isISO6346`, check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. **isFullWidth(str)** | check if the string contains any full-width chars. **isHalfWidth(str)** | check if the string contains any half-width chars. -**isHash(str, algorithm)** | check if the string is a hash of type algorithm.

Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']` +**isHash(str, algorithm)** | check if the string is a hash of type algorithm.

Algorithm is one of `['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192']`. **isHexadecimal(str)** | check if the string is a hexadecimal number. **isHexColor(str)** | check if the string is a hexadecimal color. -**isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).

Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`). -**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number). -**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['LK', 'PL', 'ES', 'FI', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN']` OR `'any'`. If 'any' is used, function will check if any of the locals match.

Defaults to 'any'. -**isIMEI(str [, options]))** | check if the string is a valid IMEI number. Imei should be of format `###############` or `##-######-######-#`.

`options` is an object which can contain the keys `allow_hyphens`. Defaults to first format . If allow_hyphens is set to true, the validator will validate the second format. -**isIn(str, values)** | check if the string is in a array of allowed values. +**isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification][CSS Colors Level 4 Specification].

Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`). +**isIBAN(str)** | check if the string is an IBAN (International Bank Account Number). +**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['LK', 'PL', 'ES', 'FI', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN', 'zh-HK']` OR `'any'`. If 'any' is used, function will check if any of the locales match.

Defaults to 'any'. +**isIMEI(str [, options]))** | check if the string is a valid [IMEI number][IMEI]. IMEI should be of format `###############` or `##-######-######-#`.

`options` is an object which can contain the keys `allow_hyphens`. Defaults to first format. If `allow_hyphens` is set to true, the validator will validate the second format. +**isIn(str, values)** | check if the string is in an array of allowed values. **isInt(str [, options])** | check if the string is an integer.

`options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4). **isIP(str [, version])** | check if the string is an IP (version 4 or 6). **isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6). -**isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13). +**isISBN(str [, options])** | check if the string is an [ISBN][ISBN].

`options` is an object that has no default.
**Options:**
`version`: ISBN version to compare to. Accepted values are '10' and '13'. If none provided, both will be tested. **isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier). **isISO6346(str)** | check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. -**isISO6391(str)** | check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code. -**isISO8601(str [, options])** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid. -**isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code. -**isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3) officially assigned country code. -**isISO4217(str)** | check if the string is a valid [ISO 4217](https://en.wikipedia.org/wiki/ISO_4217) officially assigned currency code. -**isISRC(str)** | check if the string is a [ISRC](https://en.wikipedia.org/wiki/International_Standard_Recording_Code). -**isISSN(str [, options])** | check if the string is an [ISSN](https://en.wikipedia.org/wiki/International_Standard_Serial_Number).

`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected. +**isISO6391(str)** | check if the string is a valid [ISO 639-1][ISO 639-1] language code. +**isISO8601(str [, options])** | check if the string is a valid [ISO 8601][ISO 8601] date.
`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid. +**isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2] officially assigned country code. +**isISO31661Alpha3(str)** | check if the string is a valid [ISO 3166-1 alpha-3][ISO 3166-1 alpha-3] officially assigned country code. +**isISO4217(str)** | check if the string is a valid [ISO 4217][ISO 4217] officially assigned currency code. +**isISRC(str)** | check if the string is an [ISRC][ISRC]. +**isISSN(str [, options])** | check if the string is an [ISSN][ISSN].

`options` is an object which defaults to `{ case_sensitive: false, require_hyphen: false }`. If `case_sensitive` is true, ISSNs with a lowercase `'x'` as the check digit are rejected. **isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).

`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values. **isJWT(str)** | check if the string is valid JWT token. **isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.

`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format. -**isLength(str [, options])** | check if the string's length falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs. -**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.

(locale is one of `['cs-CZ', 'de-DE', 'de-LI', 'fi-FI', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE', 'en-IN', 'hi-IN', 'gu-IN', 'as-IN', 'bn-IN', 'kn-IN', 'ml-IN', 'mr-IN', 'or-IN', 'pa-IN', 'sa-IN', 'ta-IN', 'te-IN', 'kok-IN']` or `any`) -**isLocale(str)** | check if the string is a locale +**isLength(str [, options])** | check if the string's length falls in a range.

`options` is an object which defaults to `{ min: 0, max: undefined }`. Note: this function takes into account surrogate pairs. +**isLicensePlate(str, locale)** | check if the string matches the format of a country's license plate.

`locale` is one of `['cs-CZ', 'de-DE', 'de-LI', 'en-IN', 'es-AR', 'hu-HU', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE']` or `'any'`. +**isLocale(str)** | check if the string is a locale. **isLowercase(str)** | check if the string is lowercase. -**isLuhnValid(str)** | check if the string passes the luhn check. -**isMACAddress(str [, options])** | check if the string is a MAC address.

`options` is an object which defaults to `{no_separators: false}`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'. The options also allow a `eui` property to specify if it needs to be validated against EUI-48 or EUI-64. The accepted values of `eui` are: 48, 64. -**isMagnetURI(str)** | check if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme). +**isLuhnNumber(str)** | check if the string passes the [Luhn algorithm check](https://en.wikipedia.org/wiki/Luhn_algorithm). +**isMACAddress(str [, options])** | check if the string is a MAC address.

`options` is an object which defaults to `{ no_separators: false }`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g. '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'. The options also allow a `eui` property to specify if it needs to be validated against EUI-48 or EUI-64. The accepted values of `eui` are: 48, 64. +**isMagnetURI(str)** | check if the string is a [Magnet URI format][Magnet URI Format]. **isMD5(str)** | check if the string is a MD5 hash.

Please note that you can also use the `isHash(str, 'md5')` function. Keep in mind that MD5 has some collision weaknesses compared to other algorithms (e.g., SHA). -**isMimeType(str)** | check if the string matches to a valid [MIME type](https://en.wikipedia.org/wiki/Media_type) format -**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-SY', 'ar-TN', 'ar-YE' , 'az-AZ', 'az-LY', 'az-LB', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'ca-AD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'de-LU', 'dv-MV', 'el-GR', 'el-CY' ,'en-AU', 'en-AG','en-AI', 'en-BM', 'en-BW', 'en-BS', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-GY', 'en-HK', 'en-JM', 'en-MO', 'en-IE', 'en-IN', 'en-LS', 'en-KE', 'en-KI','en-KN', 'en-MT', 'en-MU', 'en-NG', 'es-NI' , 'en-NZ', 'en-PG', 'en-PK', 'en-PH', 'en-RW', 'en-SG', 'en-SL', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-HN', 'es-PE', 'es-EC', 'es-ES', 'es-MX', 'es-PA', 'es-PY', 'es-SV', 'es-UY', 'es-VE', 'et-EE', 'fa-IR', 'fa-AF', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-BF','fr-BJ', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', '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', 'ms-MY', 'mn-MN', 'my-MM' , 'mz-MZ', nb-NO', 'ne-NP', 'nl-BE', 'nl-NL','nl-AW' 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'pt-AO', 'ro-RO', 'ru-RU', 'si-LK' 'sl-SI', 'sk-SK', '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', 'dz-BT']` 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`. +**isMimeType(str)** | check if the string matches to a valid [MIME type][MIME Type] format. +**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-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-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-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-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-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-PF', 'fr-RE', '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', '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', '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` 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', '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 }` 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', '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']`. **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', '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', 'JP', 'KR', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TH', 'TR', 'UA', 'US' ]`. +**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']`. **isPort(str)** | check if the string is a valid port number. -**isPostalCode(str, locale)** | check if the string is a postal code,

(locale is one of `[ 'AD', 'AT', 'AU', 'AZ', 'BA', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HT', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'KR', 'LI', 'LK', 'LT', 'LU', 'LV', 'MG', 'MT', 'MX', 'MY', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SG', 'SI', 'SK', 'TH', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.). -**isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date. +**isPostalCode(str, locale)** | check if the string is a postal code.

`locale` is one of `['AD', 'AT', 'AU', 'AZ', 'BA', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'KR', 'LI', 'LK', 'LT', 'LU', 'LV', 'MG', 'MT', 'MX', 'MY', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SG', 'SI', 'SK', 'TH', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM']` OR `'any'`. If 'any' is used, function will check if any of the locales match. Locale list is `validator.isPostalCodeLocales`. +**isRFC3339(str)** | check if the string is a valid [RFC 3339][RFC 3339] date. **isRgbColor(str [, includePercentValues])** | check if the string is a rgb or rgba color.

`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false. **isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer). **isSurrogatePair(str)** | check if the string contains any surrogate pairs chars. **isUppercase(str)** | check if the string is uppercase. -**isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`] -**isStrongPassword(str [, options])** | Check if a password is strong or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.
Default options:
`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }` -**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`.

More info about exact TIN support can be found in `src/lib/isTaxID.js`

Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV' 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]` -**isURL(str [, options])** | check if the string is an URL.

`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.

require_protocol - if set as true isURL will return false if protocol is not present in the URL.
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.
protocols - valid protocols can be modified with this option.
require_host - if set as false isURL will not check if host is present in the URL.
require_port - if set as true isURL will check if port is present in the URL.
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.
allow_fragments - if set as false isURL will return false if fragments are present.
allow_query_components - if set as false isURL will return false if query components are present.
validate_length - if set as false isURL will skip string length validation (2083 characters is IE max URL length). +**isSlug(str)** | check if the string is of type slug. +**isStrongPassword(str [, options])** | check if the string can be considered a strong password or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.
Default options:
`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }` +**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].

`options` is an object which can contain the keys `hourFormat` or `mode`.

`hourFormat` is a key and defaults to `'hour24'`.

`mode` is a key and defaults to `'default'`.

`hourFomat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format.

`mode` can contain the values `'default'` or `'withSeconds'`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format. +**isTaxID(str, locale)** | check if the string is a valid Tax Identification Number. Default locale is `en-US`.

More info about exact TIN support can be found in `src/lib/isTaxID.js`.

Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV', 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]`. +**isURL(str [, options])** | check if the string is a URL.

`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.

`require_protocol` - if set to true isURL will return false if protocol is not present in the URL.
`require_valid_protocol` - isURL will check if the URL's protocol is present in the protocols option.
`protocols` - valid protocols can be modified with this option.
`require_host` - if set to false isURL will not check if host is present in the URL.
`require_port` - if set to true isURL will check if port is present in the URL.
`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.
`allow_fragments` - if set to false isURL will return false if fragments are present.
`allow_query_components` - if set to false isURL will return false if query components are present.
`validate_length` - if set to false isURL will skip string length validation (2083 characters is IE max URL length). **isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4 or 5). **isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars. -**isVAT(str, countryCode)** | checks that the string is a [valid VAT number](https://en.wikipedia.org/wiki/VAT_identification_number) if validation is available for the given country code matching [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).

Available country codes: `[ 'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'EL', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'AL', 'MK', 'AU', 'BY', 'CA', 'IS', 'IN', 'ID', 'IL', 'KZ', 'NZ', 'NG', 'NO', 'PH', 'RU', 'SM', 'SA', 'RS', 'CH', 'TR', 'UA', 'GB', 'UZ', 'AR', 'BO', 'BR', 'CL', 'CO', 'CR', 'EC', 'SV', 'GT', 'HN', 'MX', 'NI', 'PA', 'PY', 'PE', 'DO', 'UY', 'VE' ]`. -**isWhitelisted(str, chars)** | checks characters if they appear in the whitelist. -**matches(str, pattern [, modifiers])** | check if string matches the pattern.

Either `matches('foo', /foo/i)` or `matches('foo', 'foo', 'i')`. +**isVAT(str, countryCode)** | check if the string is a [valid VAT number][VAT Number] if validation is available for the given country code matching [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2].

`countryCode` is one of `['AL', 'AR', 'AT', 'AU', 'BE', 'BG', 'BO', 'BR', 'BY', 'CA', 'CH', 'CL', 'CO', 'CR', 'CY', 'CZ', 'DE', 'DK', 'DO', 'EC', 'EE', 'EL', 'ES', 'FI', 'FR', 'GB', 'GT', 'HN', 'HR', 'HU', 'ID', 'IE', 'IL', 'IN', 'IS', 'IT', 'KZ', 'LT', 'LU', 'LV', 'MK', 'MT', 'MX', 'NG', 'NI', 'NL', 'NO', 'NZ', 'PA', 'PE', 'PH', 'PL', 'PT', 'PY', 'RO', 'RS', 'RU', 'SA', 'SE', 'SI', 'SK', 'SM', 'SV', 'TR', 'UA', 'UY', 'UZ', 'VE']`. +**isWhitelisted(str, chars)** | check if the string consists only of characters that appear in the whitelist `chars`. +**matches(str, pattern [, modifiers])** | check if the string matches the pattern.

Either `matches('foo', /foo/i)` or `matches('foo', 'foo', 'i')`. ## Sanitizers @@ -182,7 +183,7 @@ Sanitizer | Description **blacklist(input, chars)** | remove characters that appear in the blacklist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `blacklist(input, '\\[\\]')`. **escape(input)** | replace `<`, `>`, `&`, `'`, `"` and `/` with HTML entities. **ltrim(input [, chars])** | trim characters from the left-side of the input. -**normalizeEmail(email [, options])** | canonicalizes an email address. (This doesn't validate that the input is an email, if you want to validate the email use isEmail beforehand)

`options` is an object with the following keys and default values:
  • *all_lowercase: true* - Transforms the local part (before the @ symbol) of all email addresses to lowercase. Please note that this may violate RFC 5321, which gives providers the possibility to treat the local part of email addresses in a case sensitive way (although in practice most - yet not all - providers don't). The domain part of the email address is always lowercased, as it's case insensitive per RFC 1035.
  • *gmail_lowercase: true* - GMail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, GMail addresses are lowercased regardless of the value of this setting.
  • *gmail_remove_dots: true*: Removes dots from the local part of the email address, as GMail ignores them (e.g. "john.doe" and "johndoe" are considered equal).
  • *gmail_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@gmail.com" becomes "foo@gmail.com").
  • *gmail_convert_googlemaildotcom: true*: Converts addresses with domain @googlemail.com to @gmail.com, as they're equivalent.
  • *outlookdotcom_lowercase: true* - Outlook.com addresses (including Windows Live and Hotmail) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Outlook.com addresses are lowercased regardless of the value of this setting.
  • *outlookdotcom_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@outlook.com" becomes "foo@outlook.com").
  • *yahoo_lowercase: true* - Yahoo Mail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Yahoo Mail addresses are lowercased regardless of the value of this setting.
  • *yahoo_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "-" sign (e.g. "foo-bar@yahoo.com" becomes "foo@yahoo.com").
  • *icloud_lowercase: true* - iCloud addresses (including MobileMe) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, iCloud addresses are lowercased regardless of the value of this setting.
  • *icloud_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@icloud.com" becomes "foo@icloud.com").
+**normalizeEmail(email [, options])** | canonicalize an email address. (This doesn't validate that the input is an email, if you want to validate the email use isEmail beforehand).

`options` is an object with the following keys and default values:
  • *all_lowercase: true* - Transforms the local part (before the @ symbol) of all email addresses to lowercase. Please note that this may violate RFC 5321, which gives providers the possibility to treat the local part of email addresses in a case sensitive way (although in practice most - yet not all - providers don't). The domain part of the email address is always lowercased, as it is case insensitive per RFC 1035.
  • *gmail_lowercase: true* - Gmail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Gmail addresses are lowercased regardless of the value of this setting.
  • *gmail_remove_dots: true*: Removes dots from the local part of the email address, as Gmail ignores them (e.g. "john.doe" and "johndoe" are considered equal).
  • *gmail_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@gmail.com" becomes "foo@gmail.com").
  • *gmail_convert_googlemaildotcom: true*: Converts addresses with domain @googlemail.com to @gmail.com, as they're equivalent.
  • *outlookdotcom_lowercase: true* - Outlook.com addresses (including Windows Live and Hotmail) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Outlook.com addresses are lowercased regardless of the value of this setting.
  • *outlookdotcom_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@outlook.com" becomes "foo@outlook.com").
  • *yahoo_lowercase: true* - Yahoo Mail addresses are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, Yahoo Mail addresses are lowercased regardless of the value of this setting.
  • *yahoo_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "-" sign (e.g. "foo-bar@yahoo.com" becomes "foo@yahoo.com").
  • *icloud_lowercase: true* - iCloud addresses (including MobileMe) are known to be case-insensitive, so this switch allows lowercasing them even when *all_lowercase* is set to false. Please note that when *all_lowercase* is true, iCloud addresses are lowercased regardless of the value of this setting.
  • *icloud_remove_subaddress: true*: Normalizes addresses by removing "sub-addresses", which is the part following a "+" sign (e.g. "foo+bar@icloud.com" becomes "foo@icloud.com").
**rtrim(input [, chars])** | trim characters from the right-side of the input. **stripLow(input [, keep_new_lines])** | remove characters with a numerical value < 32 and 127, mostly control characters. If `keep_new_lines` is `true`, newline characters are preserved (`\n` and `\r`, hex `0xA` and `0xD`). Unicode-safe in JavaScript. **toBoolean(input [, strict])** | convert the input string to a boolean. Everything except for `'0'`, `'false'` and `''` returns `true`. In strict mode only `'1'` and `'true'` return `true`. @@ -190,7 +191,7 @@ Sanitizer | Description **toFloat(input)** | convert the input string to a float, or `NaN` if the input is not a float. **toInt(input [, radix])** | convert the input string to an integer, or `NaN` if the input is not an integer. **trim(input [, chars])** | trim characters (whitespace by default) from both sides of the input. -**unescape(input)** | replaces HTML encoded entities with `<`, `>`, `&`, `'`, `"` and `/`. +**unescape(input)** | replace HTML encoded entities with `<`, `>`, `&`, `'`, `"` and `/`. **whitelist(input, chars)** | remove characters that do not appear in the whitelist. The characters are used in a RegExp and so you will need to escape some chars, e.g. `whitelist(input, '\\[\\]')`. ### XSS Sanitization @@ -282,5 +283,25 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. [amd]: http://requirejs.org/docs/whyamd.html [bower]: http://bower.io/ -[mongoid]: http://docs.mongodb.org/manual/reference/object-id/ +[Crockford Base32]: http://www.crockford.com/base32.html +[Base64 URL Safe]: https://base64.guru/standards/base64url +[Data URI Format]: https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs +[European Article Number]: https://en.wikipedia.org/wiki/International_Article_Number +[Ethereum]: https://ethereum.org/ +[CSS Colors Level 4 Specification]: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value +[IMEI]: https://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity +[ISBN]: https://en.wikipedia.org/wiki/ISBN [ISIN]: https://en.wikipedia.org/wiki/International_Securities_Identification_Number +[ISO 639-1]: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes +[ISO 8601]: https://en.wikipedia.org/wiki/ISO_8601 +[ISO 3166-1 alpha-2]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 +[ISO 3166-1 alpha-3]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3 +[ISO 4217]: https://en.wikipedia.org/wiki/ISO_4217 +[ISRC]: https://en.wikipedia.org/wiki/International_Standard_Recording_Code +[ISSN]: https://en.wikipedia.org/wiki/International_Standard_Serial_Number +[Luhn Check]: https://en.wikipedia.org/wiki/Luhn_algorithm +[Magnet URI Format]: https://en.wikipedia.org/wiki/Magnet_URI_scheme +[MIME Type]: https://en.wikipedia.org/wiki/Media_type +[mongoid]: http://docs.mongodb.org/manual/reference/object-id/ +[RFC 3339]: https://tools.ietf.org/html/rfc3339 +[VAT Number]: https://en.wikipedia.org/wiki/VAT_identification_number \ No newline at end of file diff --git a/test/client-side.js b/test/clientSide.test.js similarity index 100% rename from test/client-side.js rename to test/clientSide.test.js diff --git a/test/exports.js b/test/exports.test.js similarity index 100% rename from test/exports.js rename to test/exports.test.js diff --git a/test/sanitizers.js b/test/sanitizers.test.js similarity index 100% rename from test/sanitizers.js rename to test/sanitizers.test.js diff --git a/test/testFunctions.js b/test/testFunctions.js new file mode 100644 index 000000000..bcd7c15b0 --- /dev/null +++ b/test/testFunctions.js @@ -0,0 +1,56 @@ +import assert from 'assert'; +import { format } from 'util'; +import validator from '../src/index'; + +export default function test(options) { + const args = options.args || []; + + args.unshift(null); + + if (options.error) { + options.error.forEach((error) => { + args[0] = error; + + try { + assert.throws(() => validator[options.validator](...args)); + } catch (err) { + const warning = format( + 'validator.%s(%s) passed but should error', + options.validator, args.join(', ') + ); + + throw new Error(warning); + } + }); + } + + if (options.valid) { + options.valid.forEach((valid) => { + args[0] = valid; + + if (validator[options.validator](...args) !== true) { + const warning = format( + 'validator.%s(%s) failed but should have passed', + options.validator, args.join(', ') + ); + + throw new Error(warning); + } + }); + } + + if (options.invalid) { + options.invalid.forEach((invalid) => { + args[0] = invalid; + + if (validator[options.validator](...args) !== false) { + const warning = format( + 'validator.%s(%s) passed but should have failed', + options.validator, args.join(', ') + ); + + throw new Error(warning); + } + }); + } +} diff --git a/test/util.js b/test/util.test.js similarity index 100% rename from test/util.js rename to test/util.test.js diff --git a/test/validators.js b/test/validators.test.js similarity index 96% rename from test/validators.js rename to test/validators.test.js index 4aecd1403..3a4af6174 100644 --- a/test/validators.js +++ b/test/validators.test.js @@ -3,60 +3,10 @@ import fs from 'fs'; import { format } from 'util'; import vm from 'vm'; import validator from '../src/index'; +import test from './testFunctions'; let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(); -function test(options) { - let args = options.args || []; - args.unshift(null); - if (options.error) { - options.error.forEach((error) => { - args[0] = error; - try { - assert.throws(() => validator[options.validator](...args)); - } catch (err) { - let warning = format( - 'validator.%s(%s) passed but should error', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } - if (options.valid) { - options.valid.forEach((valid) => { - args[0] = valid; - if (validator[options.validator](...args) !== true) { - let warning = format( - 'validator.%s(%s) failed but should have passed', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } - if (options.invalid) { - options.invalid.forEach((invalid) => { - args[0] = invalid; - if (validator[options.validator](...args) !== false) { - let warning = format( - 'validator.%s(%s) passed but should have failed', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } -} - -function repeat(str, count) { - let result = ''; - for (; count; count--) { - result += str; - } - return result; -} - describe('Validators', () => { it('should validate email addresses', () => { test({ @@ -74,9 +24,9 @@ describe('Validators', () => { '"foobar"@example.com', '" foo m端ller "@example.com', '"foo\\@bar"@example.com', - `${repeat('a', 64)}@${repeat('a', 63)}.com`, - `${repeat('a', 64)}@${repeat('a', 63)}.com`, - `${repeat('a', 31)}@gmail.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.com`, + `${'a'.repeat(31)}@gmail.com`, 'test@gmail.com', 'test.1@gmail.com', 'test@1337.com', @@ -90,10 +40,10 @@ describe('Validators', () => { 'foo@bar.co.uk.', 'z@co.c', 'gmailgmailgmailgmailgmail@gmail.com', - `${repeat('a', 64)}@${repeat('a', 251)}.com`, - `${repeat('a', 65)}@${repeat('a', 250)}.com`, - `${repeat('a', 64)}@${repeat('a', 64)}.com`, - `${repeat('a', 64)}@${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 58)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(251)}.com`, + `${'a'.repeat(65)}@${'a'.repeat(250)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(64)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(58)}.com`, 'test1@invalid.co m', 'test2@invalid.co m', 'test3@invalid.co m', @@ -128,10 +78,10 @@ describe('Validators', () => { 'foobar@gmail.com', 'foo.bar@gmail.com', 'foo.bar@googlemail.com', - `${repeat('a', 30)}@gmail.com`, + `${'a'.repeat(30)}@gmail.com`, ], invalid: [ - `${repeat('a', 31)}@gmail.com`, + `${'a'.repeat(31)}@gmail.com`, 'test@gmail.com', 'test.1@gmail.com', '.foobar@gmail.com', @@ -328,6 +278,15 @@ describe('Validators', () => { ], invalid: [], }); + + test({ + validator: 'isEmail', + args: [{ ignore_max_length: true }], + valid: [ + 'Deleted-user-id-19430-Team-5051deleted-user-id-19430-team-5051XXXXXX@Deleted-user-id-19430-Team-5051deleted-user-id-19430-team-5051XXXXXX.com', + ], + invalid: [], + }); }); it('should not validate email addresses with denylisted domains', () => { @@ -2905,6 +2864,18 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['AZ'], + valid: [ + 'AZE16175905', + 'AA1617595', + ], + invalid: [ + 'A12345843', + ], + }); + test({ validator: 'isPassportNumber', args: ['BE'], @@ -3240,6 +3211,18 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['JM'], + valid: [ + 'A0123456', + ], + invalid: [ + 's0123456', + 'a01234567', + ], + }); + test({ validator: 'isPassportNumber', args: ['JP'], @@ -3267,6 +3250,31 @@ describe('Validators', () => { ], }); + test({ + validator: 'isPassportNumber', + args: ['KZ'], + valid: [ + 'A0123456', + 'b0123456', + ], + invalid: [ + '01234567', + 'bb0123456', + ], + }); + + test({ + validator: 'isPassportNumber', + args: ['LI'], + valid: [ + 'a01234', + 'f01234', + ], + invalid: [ + '012345', + ], + }); + test({ validator: 'isPassportNumber', args: ['LT'], @@ -3381,6 +3389,55 @@ describe('Validators', () => { 'XR1001R58A', ], }); + test({ + validator: 'isPassportNumber', + args: ['PK'], + valid: [ + 'QZ1791293', + 'XR1001458', + ], + invalid: [ + 'XTR11013R', + 'XR1001R58A', + ], + }); + + test({ + validator: 'isPassportNumber', + args: ['PH'], + valid: [ + 'X123456', + 'XY123456', + 'XY1234567', + 'X1234567Y', + ], + invalid: [ + 'XY12345', + 'X12345Z', + 'XY12345Z', + ], + }); + + test({ + validator: 'isPassportNumber', + args: ['NZ'], + valid: [ + 'Lf012345', + 'La012345', + 'Ld012345', + 'Lh012345', + 'ea012345', + 'ep012345', + 'n012345', + ], + invalid: [ + 'Lp012345', + 'nd012345', + 'ed012345', + 'eh012345', + 'ef012345', + ], + }); test({ validator: 'isPassportNumber', @@ -4065,6 +4122,7 @@ describe('Validators', () => { ' ', '', '.', + ',', 'foo', '20.foo', '2020-01-06T14:31:00.135Z', @@ -4409,6 +4467,8 @@ describe('Validators', () => { 'rgba(3,3,3%,.3)', 'rgb(101%,101%,101%)', 'rgba(3%,3%,101%,0.3)', + 'rgb(101%,101%,101%) additional invalid string part', + 'rgba(3%,3%,101%,0.3) additional invalid string part', ], }); @@ -5043,31 +5103,6 @@ describe('Validators', () => { }); }); - it('should validate dates against a start date', () => { - test({ - validator: 'isAfter', - args: ['2011-08-03'], - valid: ['2011-08-04', new Date(2011, 8, 10).toString()], - invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'], - }); - test({ - validator: 'isAfter', - valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], - invalid: ['2010-07-02', new Date(0).toString()], - }); - test({ - validator: 'isAfter', - args: ['2011-08-03'], - valid: ['2015-09-17'], - invalid: ['invalid date'], - }); - test({ - validator: 'isAfter', - args: ['invalid date'], - invalid: ['invalid date', '2015-09-17'], - }); - }); - it('should validate dates against an end date', () => { test({ validator: 'isBefore', @@ -5148,6 +5183,7 @@ describe('Validators', () => { 'SBICKEN1', 'SBICKENY', 'SBICKEN1YYP', + 'SBICXKN1YYP', ], invalid: [ 'SBIC23NXXX', @@ -5156,6 +5192,7 @@ describe('Validators', () => { 'SBICKENXX9', 'SBICKEN13458', 'SBICKEN', + 'SBICXK', ], }); }); @@ -5178,7 +5215,7 @@ describe('Validators', () => { it('should validate luhn numbers', () => { test({ - validator: 'isLuhnValid', + validator: 'isLuhnNumber', valid: [ '0', '5421', @@ -5518,6 +5555,29 @@ describe('Validators', () => { it('should validate identity cards', () => { const fixtures = [ + { + locale: 'zh-HK', + valid: [ + 'OV290326[A]', + 'Q803337[0]', + 'Z0977986', + 'W520128(7)', + 'A494866[4]', + 'A494866(4)', + 'Z867821A', + 'ag293013(9)', + 'k348609(5)', + ], + invalid: [ + 'A1234567890', + '98765432', + 'O962472(9)', + 'M4578601', + 'X731324[8]', + 'C503134(5)', + 'RH265886(3)', + ], + }, { locale: 'LK', valid: [ @@ -5910,58 +5970,6 @@ describe('Validators', () => { }); }); - it('should validate ISBNs', () => { - test({ - validator: 'isISBN', - args: [10], - valid: [ - '3836221195', '3-8362-2119-5', '3 8362 2119 5', - '1617290858', '1-61729-085-8', '1 61729 085-8', - '0007269706', '0-00-726970-6', '0 00 726970 6', - '3423214120', '3-423-21412-0', '3 423 21412 0', - '340101319X', '3-401-01319-X', '3 401 01319 X', - ], - invalid: [ - '3423214121', '3-423-21412-1', '3 423 21412 1', - '978-3836221191', '9783836221191', - '123456789a', 'foo', '', - ], - }); - test({ - validator: 'isISBN', - args: [13], - valid: [ - '9783836221191', '978-3-8362-2119-1', '978 3 8362 2119 1', - '9783401013190', '978-3401013190', '978 3401013190', - '9784873113685', '978-4-87311-368-5', '978 4 87311 368 5', - ], - invalid: [ - '9783836221190', '978-3-8362-2119-0', '978 3 8362 2119 0', - '3836221195', '3-8362-2119-5', '3 8362 2119 5', - '01234567890ab', 'foo', '', - ], - }); - test({ - validator: 'isISBN', - valid: [ - '340101319X', - '9784873113685', - ], - invalid: [ - '3423214121', - '9783836221190', - ], - }); - test({ - validator: 'isISBN', - args: ['foo'], - invalid: [ - '340101319X', - '9784873113685', - ], - }); - }); - it('should validate EANs', () => { test({ validator: 'isEAN', @@ -6582,9 +6590,11 @@ describe('Validators', () => { '96550000000', '96560000000', '96590000000', + '96541000000', '+96550000000', '+96550000220', '+96551111220', + '+96541000000', ], invalid: [ '+96570000220', @@ -6595,6 +6605,7 @@ describe('Validators', () => { '+9639626626262', '+963332210972', '0114152198', + '+96540000000', ], }, { @@ -7155,6 +7166,7 @@ describe('Validators', () => { '+4418970973', '', '+1441897465', + '+1441897465 additional invalid string part', ], }, { @@ -7587,6 +7599,24 @@ describe('Validators', () => { '11435213543', ], }, + { + locale: 'fr-CD', + valid: [ + '+243818590432', + '+243893875610', + '243978590234', + '0813346543', + '0820459022', + '+243902590221', + ], + invalid: [ + '243', + '+254818590432', + '+24389032', + '123456789', + '+243700723845', + ], + }, { locale: 'fr-GF', valid: [ @@ -7735,8 +7765,22 @@ describe('Validators', () => { locale: 'el-GR', valid: [ '+306944848966', - '6944848966', '306944848966', + '06904567890', + '6944848966', + '6904567890', + '6914567890', + '6934567890', + '6944567890', + '6954567890', + '6974567890', + '6984567890', + '6994567890', + '6854567890', + '6864567890', + '6874567890', + '6884567890', + '6894567890', ], invalid: [ '2102323234', @@ -7746,6 +7790,12 @@ describe('Validators', () => { '68129485729', '6589394827', '298RI89572', + '6924567890', + '6964567890', + '6844567890', + '690456789', + '00690456789', + 'not a number', ], }, { @@ -8254,6 +8304,10 @@ describe('Validators', () => { '+50489234567', '+50488987896', '+50497567389', + '+50427367389', + '+50422357389', + '+50431257389', + '+50430157389', ], invalid: [ '12345', @@ -8593,6 +8647,8 @@ describe('Validators', () => { '088-261987', '1800-88-8687', '088-320000', + '+01112353576', + '+0111419752', ], }, { @@ -8830,6 +8886,36 @@ describe('Validators', () => { 'number', ], }, + { + locale: 'ro-MD', + valid: [ + '+37360375781', + '+37361945673', + '+37362387563', + '+37368447788', + '+37369000101', + '+37367568910', + '+37376758294', + '+37378457892', + '+37379067436', + '37362387563', + '37368447788', + '37369000101', + '37367568910', + ], + invalid: [ + '', + '+37363373381', + '+37364310581', + '+37365578199', + '+37371088636', + 'Vml2YW11cyBmZXJtZtesting123', + '123456', + '740123456', + '+40640123456', + '+40210123456', + ], + }, { locale: 'ro-RO', valid: [ @@ -8856,6 +8942,8 @@ describe('Validators', () => { '740123456', '+40640123456', '+40210123456', + '+0765351689', + '+0711419752', ], }, { @@ -9136,35 +9224,6 @@ describe('Validators', () => { '998900066506', ], }, - { - locale: ['en-ZA', 'be-BY'], - valid: [ - '0821231234', - '+27821231234', - '27821231234', - '+375241234567', - '+375251234567', - '+375291234567', - '+375331234567', - '+375441234567', - '375331234567', - ], - invalid: [ - '082123', - '08212312345', - '21821231234', - '+21821231234', - '+0821231234', - '12345', - '', - 'ASDFGJKLmZXJtZtesting123', - '010-38238383', - '+9676338855', - '19676338855', - '6676338855', - '+99676338855', - ], - }, { locale: 'en-SL', valid: [ @@ -9524,18 +9583,30 @@ describe('Validators', () => { { locale: 'dv-MV', valid: [ - '+960973256874', - '781246378', - '+960766354789', - '+960912354789', + '+9609112345', + '+9609958973', + '+9607258963', + '+9607958463', + '9609112345', + '9609958973', + '9607212963', + '9607986963', + '9112345', + '9958973', + '7258963', + '7958963', ], invalid: [ '+96059234567', '+96045789', '7812463784', - '+960706985478', - '+960926985478', 'NotANumber', + '+9607112345', + '+9609012345', + '+609012345', + '+96071123456', + '3412345', + '9603412345', ], }, { @@ -9598,6 +9669,33 @@ describe('Validators', () => { '00 +503 1234 5678', ], }, + { + locale: 'en-SS', + valid: [ + '+211928530422', + '+211913384561', + '+211972879174', + '+211952379334', + '0923346543', + '0950459022', + '0970934567', + '211979841238', + '211929843238', + '211959840238', + ], + invalid: [ + '911', + '+211999', + '123456789909', + 'South Sudan', + '21195 840 238', + '+211981234567', + '+211931234567', + '+211901234567', + '+211991234567', + + ], + }, ]; let allValid = []; @@ -11096,6 +11194,7 @@ describe('Validators', () => { valid: [ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC', + 'data:application/media_control+xml;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC', ' data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIBAMAAAA2IaO4AAAAFVBMVEXk5OTn5+ft7e319fX29vb5+fn///++GUmVAAAALUlEQVQIHWNICnYLZnALTgpmMGYIFWYIZTA2ZFAzTTFlSDFVMwVyQhmAwsYMAKDaBy0axX/iAAAAAElFTkSuQmCC ', 'data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%22%20height%3D%22100%22%3E%3Crect%20fill%3D%22%2300B1FF%22%20width%3D%22100%22%20height%3D%22100%22%2F%3E%3C%2Fsvg%3E', 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj48cmVjdCBmaWxsPSIjMDBCMUZGIiB3aWR0aD0iMTAwIiBoZWlnaHQ9IjEwMCIvPjwvc3ZnPg==', @@ -11262,6 +11361,12 @@ describe('Validators', () => { '247710', '231960', ], + invalid: [ + 'test 225320', + '211120 test', + '317543', + '267946', + ], }, { locale: 'CA', @@ -11447,6 +11552,9 @@ describe('Validators', () => { '43516 6456', '123443516 6456', '891123', + 'test 4351666456', + '4351666456 test', + 'test 4351666456 test', ], }, { @@ -11801,6 +11909,7 @@ describe('Validators', () => { 'font/woff2', 'message/http', 'model/vnd.gtw', + 'application/media_control+xml', 'multipart/form-data', 'multipart/form-data; boundary=something', 'multipart/form-data; charset=utf-8; boundary=something', @@ -12476,6 +12585,7 @@ describe('Validators', () => { 'mxH_+2vs&54_+H3P', '+&DxJ=X7-4L8jRCD', 'etV*p%Nr6w&H%FeF', + '£3.ndSau_7', ], invalid: [ '', @@ -12678,7 +12788,134 @@ describe('Validators', () => { ], }); }); + it('should validate time', () => { + test({ + validator: 'isTime', + valid: [ + '00:00', + '23:59', + '9:00', + ], + invalid: [ + '', + null, + undefined, + 0, + '07:00 PM', + '23', + '00:60', + '00:', + '01:0 ', + '001:01', + ], + }); + test({ + validator: 'isTime', + args: [{ hourFormat: 'hour24', mode: 'withSeconds' }], + valid: [ + '23:59:59', + '00:00:00', + '9:50:01', + ], + invalid: [ + '', + null, + undefined, + 23, + '01:00:01 PM', + '13:00:', + '00', + '26', + '00;01', + '0 :09', + '59:59:59', + '24:00:00', + '00:59:60', + '99:99:99', + '009:50:01', + ], + }); + test({ + validator: 'isTime', + args: [{ hourFormat: 'hour12' }], + valid: [ + '12:59 PM', + '12:59 AM', + '01:00 PM', + '01:00 AM', + '7:00 AM', + ], + invalid: [ + '', + null, + undefined, + 0, + '12:59 MM', + '12:59 MA', + '12:59 PA', + '12:59 A M', + '13:00 PM', + '23', + '00:60', + '00:', + '9:00', + '01:0 ', + '001:01', + '12:59:00 PM', + '12:59:00 A M', + '12:59:00 ', + ], + }); + test({ + validator: 'isTime', + args: [{ hourFormat: 'hour12', mode: 'withSeconds' }], + valid: [ + '12:59:59 PM', + '2:34:45 AM', + '7:00:00 AM', + ], + invalid: [ + '', + null, + undefined, + 23, + '01:00: 1 PM', + '13:00:', + '13:00:00 PM', + '00', + '26', + '00;01', + '0 :09', + '59:59:59', + '24:00:00', + '00:59:60', + '99:99:99', + '9:50:01', + '009:50:01', + ], + }); + }); it('should be valid license plate', () => { + test({ + validator: 'isLicensePlate', + args: ['es-AR'], + valid: [ + 'AB 123 CD', + 'AB123CD', + 'ABC 123', + 'ABC123', + ], + invalid: [ + '', + 'notalicenseplate', + 'AB-123-CD', + 'ABC-123', + 'AABC 123', + 'AB CDE FG', + 'ABC DEF', + '12 ABC 34', + ], + }); test({ validator: 'isLicensePlate', args: ['pt-PT'], @@ -12857,6 +13094,76 @@ describe('Validators', () => { 'AB123DC', ], }); + test({ + validator: 'isLicensePlate', + args: ['hu-HU'], + valid: [ + 'AAB-001', + 'AVC-987', + 'KOC-124', + 'JCM-871', + 'AWQ-777', + 'BPO-001', + 'BPI-002', + 'UCO-342', + 'UDO-385', + 'XAO-987', + 'AAI-789', + 'ABI-789', + 'ACI-789', + 'AAO-789', + 'ABO-789', + 'ACO-789', + 'YAA-123', + 'XAA-123', + 'WAA-258', + 'XZZ-784', + 'M123456', + 'CK 12-34', + 'DT 12-34', + 'CD 12-34', + 'HC 12-34', + 'HB 12-34', + 'HK 12-34', + 'MA 12-34', + 'OT 12-34', + 'RR 17-87', + 'CD 124-348', + 'C-C 2021', + 'C-X 2458', + 'X-A 7842', + 'E-72345', + 'Z-07458', + 'S ACF 83', + 'SP 04-68', + ], + invalid: [ + 'AAA-547', + 'aab-001', + 'AAB 001', + 'AB34', + '789-LKJ', + 'BBO-987', + 'BBI-987', + 'BWQ-777', + 'BQW-987', + 'BAI-789', + 'BBI-789', + 'BCI-789', + 'BAO-789', + 'BBO-789', + 'BCO-789', + 'ADI-789', + 'ADO-789', + 'KOC-1234', + 'M1234567', + 'W-12345', + 'S BCF 83', + 'X-D 1234', + 'C-D 1234', + 'HU 12-34', + ], + }); test({ validator: 'isLicensePlate', args: ['any'], diff --git a/test/validators/isAfter.test.js b/test/validators/isAfter.test.js new file mode 100644 index 000000000..d771d9198 --- /dev/null +++ b/test/validators/isAfter.test.js @@ -0,0 +1,61 @@ +import test from '../testFunctions'; + +describe('isAfter', () => { + it('should validate dates against a start date', () => { + test({ + validator: 'isAfter', + args: [{ comparisonDate: '2011-08-03' }], + valid: ['2011-08-04', new Date(2011, 8, 10).toString()], + invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'], + }); + + test({ + validator: 'isAfter', + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + invalid: ['2010-07-02', new Date(0).toString()], + }); + + test({ + validator: 'isAfter', + args: [{ comparisonDate: '2011-08-03' }], + valid: ['2015-09-17'], + invalid: ['invalid date'], + }); + + test({ + validator: 'isAfter', + args: [{ comparisonDate: 'invalid date' }], + invalid: ['invalid date', '2015-09-17'], + }); + }); + + describe('(legacy syntax)', () => { + it('should validate dates against a start date', () => { + test({ + validator: 'isAfter', + args: ['2011-08-03'], + valid: ['2011-08-04', new Date(2011, 8, 10).toString()], + invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'], + }); + + test({ + validator: 'isAfter', + valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()], + invalid: ['2010-07-02', new Date(0).toString()], + }); + + test({ + validator: 'isAfter', + args: ['2011-08-03'], + valid: ['2015-09-17'], + invalid: ['invalid date'], + }); + + test({ + validator: 'isAfter', + args: ['invalid date'], + invalid: ['invalid date', '2015-09-17'], + }); + }); + }); +}); diff --git a/test/validators/isISBN.test.js b/test/validators/isISBN.test.js new file mode 100644 index 000000000..99fb2e014 --- /dev/null +++ b/test/validators/isISBN.test.js @@ -0,0 +1,109 @@ +import test from '../testFunctions'; + +describe('isISBN', () => { + it('should validate ISBNs', () => { + test({ + validator: 'isISBN', + args: [{ version: 10 }], + valid: [ + '3836221195', '3-8362-2119-5', '3 8362 2119 5', + '1617290858', '1-61729-085-8', '1 61729 085-8', + '0007269706', '0-00-726970-6', '0 00 726970 6', + '3423214120', '3-423-21412-0', '3 423 21412 0', + '340101319X', '3-401-01319-X', '3 401 01319 X', + ], + invalid: [ + '3423214121', '3-423-21412-1', '3 423 21412 1', + '978-3836221191', '9783836221191', + '123456789a', 'foo', '', + ], + }); + test({ + validator: 'isISBN', + args: [{ version: 13 }], + valid: [ + '9783836221191', '978-3-8362-2119-1', '978 3 8362 2119 1', + '9783401013190', '978-3401013190', '978 3401013190', + '9784873113685', '978-4-87311-368-5', '978 4 87311 368 5', + ], + invalid: [ + '9783836221190', '978-3-8362-2119-0', '978 3 8362 2119 0', + '3836221195', '3-8362-2119-5', '3 8362 2119 5', + '01234567890ab', 'foo', '', + ], + }); + test({ + validator: 'isISBN', + valid: [ + '340101319X', + '9784873113685', + ], + invalid: [ + '3423214121', + '9783836221190', + ], + }); + test({ + validator: 'isISBN', + args: [{ version: 'foo' }], + invalid: [ + '340101319X', + '9784873113685', + ], + }); + }); + + describe('(legacy syntax)', () => { + it('should validate ISBNs', () => { + test({ + validator: 'isISBN', + args: [10], + valid: [ + '3836221195', '3-8362-2119-5', '3 8362 2119 5', + '1617290858', '1-61729-085-8', '1 61729 085-8', + '0007269706', '0-00-726970-6', '0 00 726970 6', + '3423214120', '3-423-21412-0', '3 423 21412 0', + '340101319X', '3-401-01319-X', '3 401 01319 X', + ], + invalid: [ + '3423214121', '3-423-21412-1', '3 423 21412 1', + '978-3836221191', '9783836221191', + '123456789a', 'foo', '', + ], + }); + test({ + validator: 'isISBN', + args: [13], + valid: [ + '9783836221191', '978-3-8362-2119-1', '978 3 8362 2119 1', + '9783401013190', '978-3401013190', '978 3401013190', + '9784873113685', '978-4-87311-368-5', '978 4 87311 368 5', + ], + invalid: [ + '9783836221190', '978-3-8362-2119-0', '978 3 8362 2119 0', + '3836221195', '3-8362-2119-5', '3 8362 2119 5', + '01234567890ab', 'foo', '', + ], + }); + test({ + validator: 'isISBN', + valid: [ + '340101319X', + '9784873113685', + ], + invalid: [ + '3423214121', + '9783836221190', + ], + }); + test({ + validator: 'isISBN', + args: ['foo'], + invalid: [ + '340101319X', + '9784873113685', + ], + }); + }); + }); +}); From ffe6c7c434b4d9931e6f037c8b6be475adc0529a Mon Sep 17 00:00:00 2001 From: Songyue Wang Date: Mon, 6 Feb 2023 13:11:08 +0800 Subject: [PATCH 6/6] Fixed M/C --- package.json | 4 +- src/index.js | 8 ++-- src/lib/isAfter.js | 12 +++-- src/lib/isBIC.js | 4 +- src/lib/isBtcAddress.js | 7 +-- src/lib/isCreditCard.js | 2 +- src/lib/isDataURI.js | 2 +- src/lib/isEmail.js | 5 +- src/lib/isFQDN.js | 3 +- src/lib/isFloat.js | 2 +- src/lib/isISBN.js | 52 +++++++++++++-------- src/lib/isIdentityCard.js | 31 ++++++++++++ src/lib/isLicensePlate.js | 24 +++------- src/lib/{isLuhnValid.js => isLuhnNumber.js} | 2 +- src/lib/isMimeType.js | 2 +- src/lib/isMobilePhone.js | 17 ++++--- src/lib/isPassportNumber.js | 9 +++- src/lib/isPostalCode.js | 4 +- src/lib/isRgbColor.js | 4 +- src/lib/isStrongPassword.js | 2 +- src/lib/isTime.js | 23 +++++++++ src/lib/toDate.js | 1 + 22 files changed, 145 insertions(+), 75 deletions(-) rename src/lib/{isLuhnValid.js => isLuhnNumber.js} (93%) create mode 100644 src/lib/isTime.js diff --git a/package.json b/package.json index 7d505205e..4a1034945 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "validator", "description": "String validation and sanitization", - "version": "13.7.0", + "version": "13.9.0", "sideEffects": false, "homepage": "https://github.com/validatorjs/validator.js", "files": [ @@ -66,7 +66,7 @@ "build:node": "babel src -d .", "build": "run-p build:*", "pretest": "npm run build && npm run lint", - "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot" + "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot --recursive" }, "engines": { "node": ">= 0.10" diff --git a/src/index.js b/src/index.js index e6ca22bbe..07cbbefbe 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ import isIP from './lib/isIP'; import isIPRange from './lib/isIPRange'; import isFQDN from './lib/isFQDN'; import isDate from './lib/isDate'; +import isTime from './lib/isTime'; import isBoolean from './lib/isBoolean'; import isLocale from './lib/isLocale'; @@ -69,7 +70,7 @@ import isBefore from './lib/isBefore'; import isIn from './lib/isIn'; -import isLuhnValid from './lib/isLuhnValid'; +import isLuhnNumber from './lib/isLuhnNumber'; import isCreditCard from './lib/isCreditCard'; import isIdentityCard from './lib/isIdentityCard'; @@ -124,7 +125,7 @@ import isStrongPassword from './lib/isStrongPassword'; import isVAT from './lib/isVAT'; -const version = '13.7.0'; +const version = '13.9.0'; const validator = { version, @@ -185,7 +186,7 @@ const validator = { isAfter, isBefore, isIn, - isLuhnValid, + isLuhnNumber, isCreditCard, isIdentityCard, isEAN, @@ -229,6 +230,7 @@ const validator = { isStrongPassword, isTaxID, isDate, + isTime, isLicensePlate, isVAT, ibanLocales, diff --git a/src/lib/isAfter.js b/src/lib/isAfter.js index 47bfb537f..e116e77ce 100644 --- a/src/lib/isAfter.js +++ b/src/lib/isAfter.js @@ -1,9 +1,11 @@ -import assertString from './util/assertString'; import toDate from './toDate'; -export default function isAfter(str, date = String(new Date())) { - assertString(str); - const comparison = toDate(date); - const original = toDate(str); +export default function isAfter(date, options) { + // For backwards compatibility: + // isAfter(str [, date]), i.e. `options` could be used as argument for the legacy `date` + const comparisonDate = options?.comparisonDate || options || Date().toString(); + + const comparison = toDate(comparisonDate); + const original = toDate(date); return !!(original && comparison && original > comparison); } diff --git a/src/lib/isBIC.js b/src/lib/isBIC.js index b5576b24e..b0f586728 100644 --- a/src/lib/isBIC.js +++ b/src/lib/isBIC.js @@ -9,7 +9,9 @@ export default function isBIC(str) { // toUpperCase() should be removed when a new major version goes out that changes // the regex to [A-Z] (per the spec). - if (!CountryCodes.has(str.slice(4, 6).toUpperCase())) { + const countryCode = str.slice(4, 6).toUpperCase(); + + if (!CountryCodes.has(countryCode) && countryCode !== 'XK') { return false; } diff --git a/src/lib/isBtcAddress.js b/src/lib/isBtcAddress.js index 2dfd04651..08f12f4ca 100644 --- a/src/lib/isBtcAddress.js +++ b/src/lib/isBtcAddress.js @@ -1,14 +1,9 @@ import assertString from './util/assertString'; -// supports Bech32 addresses const bech32 = /^(bc1)[a-z0-9]{25,39}$/; const base58 = /^(1|3)[A-HJ-NP-Za-km-z1-9]{25,39}$/; export default function isBtcAddress(str) { assertString(str); - // check for bech32 - if (str.startsWith('bc1')) { - return bech32.test(str); - } - return base58.test(str); + return bech32.test(str) || base58.test(str); } diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js index 0a874c9cf..b7b24e968 100644 --- a/src/lib/isCreditCard.js +++ b/src/lib/isCreditCard.js @@ -1,5 +1,5 @@ import assertString from './util/assertString'; -import isLuhnValid from './isLuhnValid'; +import isLuhnValid from './isLuhnNumber'; const cards = { amex: /^3[47][0-9]{13}$/, diff --git a/src/lib/isDataURI.js b/src/lib/isDataURI.js index 0275d2cc8..506544807 100644 --- a/src/lib/isDataURI.js +++ b/src/lib/isDataURI.js @@ -1,6 +1,6 @@ import assertString from './util/assertString'; -const validMediaType = /^[a-z]+\/[a-z0-9\-\+\.]+$/i; +const validMediaType = /^[a-z]+\/[a-z0-9\-\+\._]+$/i; const validAttribute = /^[a-z\-]+=[a-z0-9\-]+$/i; diff --git a/src/lib/isEmail.js b/src/lib/isEmail.js index 6db00195c..d1c35bd46 100644 --- a/src/lib/isEmail.js +++ b/src/lib/isEmail.js @@ -139,7 +139,10 @@ export default function isEmail(str, options) { return false; } - if (!isFQDN(domain, { require_tld: options.require_tld })) { + if (!isFQDN(domain, { + require_tld: options.require_tld, + ignore_max_length: options.ignore_max_length, + })) { if (!options.allow_ip_domain) { return false; } diff --git a/src/lib/isFQDN.js b/src/lib/isFQDN.js index 884d1dd6f..eb6928fda 100644 --- a/src/lib/isFQDN.js +++ b/src/lib/isFQDN.js @@ -7,6 +7,7 @@ const default_fqdn_options = { allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, + ignore_max_length: false, }; export default function isFQDN(str, options) { @@ -48,7 +49,7 @@ export default function isFQDN(str, options) { } return parts.every((part) => { - if (part.length > 63) { + if (part.length > 63 && !options.ignore_max_length) { return false; } diff --git a/src/lib/isFloat.js b/src/lib/isFloat.js index e6cced044..643f9729f 100644 --- a/src/lib/isFloat.js +++ b/src/lib/isFloat.js @@ -5,7 +5,7 @@ export default function isFloat(str, options) { assertString(str); options = options || {}; const float = new RegExp(`^(?:[-+])?(?:[0-9]+)?(?:\\${options.locale ? decimal[options.locale] : '.'}[0-9]*)?(?:[eE][\\+\\-]?(?:[0-9]+))?$`); - if (str === '' || str === '.' || str === '-' || str === '+') { + if (str === '' || str === '.' || str === ',' || str === '-' || str === '+') { return false; } const value = parseFloat(str.replace(',', '.')); diff --git a/src/lib/isISBN.js b/src/lib/isISBN.js index c66c4c991..4499c59a0 100644 --- a/src/lib/isISBN.js +++ b/src/lib/isISBN.js @@ -1,43 +1,55 @@ import assertString from './util/assertString'; -const isbn10Maybe = /^(?:[0-9]{9}X|[0-9]{10})$/; -const isbn13Maybe = /^(?:[0-9]{13})$/; +const possibleIsbn10 = /^(?:[0-9]{9}X|[0-9]{10})$/; +const possibleIsbn13 = /^(?:[0-9]{13})$/; const factor = [1, 3]; -export default function isISBN(str, version = '') { - assertString(str); - version = String(version); - if (!version) { - return isISBN(str, 10) || isISBN(str, 13); +export default function isISBN(isbn, options) { + assertString(isbn); + + // For backwards compatibility: + // isISBN(str [, version]), i.e. `options` could be used as argument for the legacy `version` + const version = String(options?.version || options); + + if (!(options?.version || options)) { + return isISBN(isbn, { version: 10 }) || isISBN(isbn, { version: 13 }); } - const sanitized = str.replace(/[\s-]+/g, ''); + + const sanitizedIsbn = isbn.replace(/[\s-]+/g, ''); + let checksum = 0; - let i; + if (version === '10') { - if (!isbn10Maybe.test(sanitized)) { + if (!possibleIsbn10.test(sanitizedIsbn)) { return false; } - for (i = 0; i < 9; i++) { - checksum += (i + 1) * sanitized.charAt(i); + + for (let i = 0; i < version - 1; i++) { + checksum += (i + 1) * sanitizedIsbn.charAt(i); } - if (sanitized.charAt(9) === 'X') { + + if (sanitizedIsbn.charAt(9) === 'X') { checksum += 10 * 10; } else { - checksum += 10 * sanitized.charAt(9); + checksum += 10 * sanitizedIsbn.charAt(9); } + if ((checksum % 11) === 0) { - return !!sanitized; + return true; } } else if (version === '13') { - if (!isbn13Maybe.test(sanitized)) { + if (!possibleIsbn13.test(sanitizedIsbn)) { return false; } - for (i = 0; i < 12; i++) { - checksum += factor[i % 2] * sanitized.charAt(i); + + for (let i = 0; i < 12; i++) { + checksum += factor[i % 2] * sanitizedIsbn.charAt(i); } - if (sanitized.charAt(12) - ((10 - (checksum % 10)) % 10) === 0) { - return !!sanitized; + + if (sanitizedIsbn.charAt(12) - ((10 - (checksum % 10)) % 10) === 0) { + return true; } } + return false; } diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index d34ddae26..4734b7bd9 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -342,6 +342,37 @@ const validators = { }; return checkIdCardNo(str); }, + 'zh-HK': (str) => { + // sanitize user input + str = str.trim(); + + // HKID number starts with 1 or 2 letters, followed by 6 digits, + // then a checksum contained in square / round brackets or nothing + const regexHKID = /^[A-Z]{1,2}[0-9]{6}((\([0-9A]\))|(\[[0-9A]\])|([0-9A]))$/; + const regexIsDigit = /^[0-9]$/; + + // convert the user input to all uppercase and apply regex + str = str.toUpperCase(); + if (!regexHKID.test(str)) return false; + str = str.replace(/\[|\]|\(|\)/g, ''); + + if (str.length === 8) str = `3${str}`; + let checkSumVal = 0; + for (let i = 0; i <= 7; i++) { + let convertedChar; + if (!regexIsDigit.test(str[i])) convertedChar = (str[i].charCodeAt(0) - 55) % 11; + else convertedChar = str[i]; + checkSumVal += (convertedChar * (9 - i)); + } + checkSumVal %= 11; + + let checkSumConverted; + if (checkSumVal === 0) checkSumConverted = '0'; + else if (checkSumVal === 1) checkSumConverted = 'A'; + else checkSumConverted = String(11 - checkSumVal); + if (checkSumConverted === str[str.length - 1]) return true; + return false; + }, 'zh-TW': (str) => { const ALPHABET_CODES = { A: 10, diff --git a/src/lib/isLicensePlate.js b/src/lib/isLicensePlate.js index e370e5029..48f8ebe99 100644 --- a/src/lib/isLicensePlate.js +++ b/src/lib/isLicensePlate.js @@ -2,36 +2,24 @@ import assertString from './util/assertString'; const validators = { 'cs-CZ': str => - /^(([ABCDEFHKIJKLMNPRSTUVXYZ]|[0-9])-?){5,8}$/.test(str), + /^(([ABCDEFHIJKLMNPRSTUVXYZ]|[0-9])-?){5,8}$/.test(str), 'de-DE': str => /^((A|AA|AB|AC|AE|AH|AK|AM|AN|AÖ|AP|AS|AT|AU|AW|AZ|B|BA|BB|BC|BE|BF|BH|BI|BK|BL|BM|BN|BO|BÖ|BS|BT|BZ|C|CA|CB|CE|CO|CR|CW|D|DA|DD|DE|DH|DI|DL|DM|DN|DO|DU|DW|DZ|E|EA|EB|ED|EE|EF|EG|EH|EI|EL|EM|EN|ER|ES|EU|EW|F|FB|FD|FF|FG|FI|FL|FN|FO|FR|FS|FT|FÜ|FW|FZ|G|GA|GC|GD|GE|GF|GG|GI|GK|GL|GM|GN|GÖ|GP|GR|GS|GT|GÜ|GV|GW|GZ|H|HA|HB|HC|HD|HE|HF|HG|HH|HI|HK|HL|HM|HN|HO|HP|HR|HS|HU|HV|HX|HY|HZ|IK|IL|IN|IZ|J|JE|JL|K|KA|KB|KC|KE|KF|KG|KH|KI|KK|KL|KM|KN|KO|KR|KS|KT|KU|KW|KY|L|LA|LB|LC|LD|LF|LG|LH|LI|LL|LM|LN|LÖ|LP|LR|LU|M|MA|MB|MC|MD|ME|MG|MH|MI|MK|ML|MM|MN|MO|MQ|MR|MS|MÜ|MW|MY|MZ|N|NB|ND|NE|NF|NH|NI|NK|NM|NÖ|NP|NR|NT|NU|NW|NY|NZ|OA|OB|OC|OD|OE|OF|OG|OH|OK|OL|OP|OS|OZ|P|PA|PB|PE|PF|PI|PL|PM|PN|PR|PS|PW|PZ|R|RA|RC|RD|RE|RG|RH|RI|RL|RM|RN|RO|RP|RS|RT|RU|RV|RW|RZ|S|SB|SC|SE|SG|SI|SK|SL|SM|SN|SO|SP|SR|ST|SU|SW|SY|SZ|TE|TF|TG|TO|TP|TR|TS|TT|TÜ|ÜB|UE|UH|UL|UM|UN|V|VB|VG|VK|VR|VS|W|WA|WB|WE|WF|WI|WK|WL|WM|WN|WO|WR|WS|WT|WÜ|WW|WZ|Z|ZE|ZI|ZP|ZR|ZW|ZZ)[- ]?[A-Z]{1,2}[- ]?\d{1,4}|(ABG|ABI|AIB|AIC|ALF|ALZ|ANA|ANG|ANK|APD|ARN|ART|ASL|ASZ|AUR|AZE|BAD|BAR|BBG|BCH|BED|BER|BGD|BGL|BID|BIN|BIR|BIT|BIW|BKS|BLB|BLK|BNA|BOG|BOH|BOR|BOT|BRA|BRB|BRG|BRK|BRL|BRV|BSB|BSK|BTF|BÜD|BUL|BÜR|BÜS|BÜZ|CAS|CHA|CLP|CLZ|COC|COE|CUX|DAH|DAN|DAU|DBR|DEG|DEL|DGF|DIL|DIN|DIZ|DKB|DLG|DON|DUD|DÜW|EBE|EBN|EBS|ECK|EIC|EIL|EIN|EIS|EMD|EMS|ERB|ERH|ERK|ERZ|ESB|ESW|FDB|FDS|FEU|FFB|FKB|FLÖ|FOR|FRG|FRI|FRW|FTL|FÜS|GAN|GAP|GDB|GEL|GEO|GER|GHA|GHC|GLA|GMN|GNT|GOA|GOH|GRA|GRH|GRI|GRM|GRZ|GTH|GUB|GUN|GVM|HAB|HAL|HAM|HAS|HBN|HBS|HCH|HDH|HDL|HEB|HEF|HEI|HER|HET|HGN|HGW|HHM|HIG|HIP|HMÜ|HOG|HOH|HOL|HOM|HOR|HÖS|HOT|HRO|HSK|HST|HVL|HWI|IGB|ILL|JÜL|KEH|KEL|KEM|KIB|KLE|KLZ|KÖN|KÖT|KÖZ|KRU|KÜN|KUS|KYF|LAN|LAU|LBS|LBZ|LDK|LDS|LEO|LER|LEV|LIB|LIF|LIP|LÖB|LOS|LRO|LSZ|LÜN|LUP|LWL|MAB|MAI|MAK|MAL|MED|MEG|MEI|MEK|MEL|MER|MET|MGH|MGN|MHL|MIL|MKK|MOD|MOL|MON|MOS|MSE|MSH|MSP|MST|MTK|MTL|MÜB|MÜR|MYK|MZG|NAB|NAI|NAU|NDH|NEA|NEB|NEC|NEN|NES|NEW|NMB|NMS|NOH|NOL|NOM|NOR|NVP|NWM|OAL|OBB|OBG|OCH|OHA|ÖHR|OHV|OHZ|OPR|OSL|OVI|OVL|OVP|PAF|PAN|PAR|PCH|PEG|PIR|PLÖ|PRÜ|QFT|QLB|RDG|REG|REH|REI|RID|RIE|ROD|ROF|ROK|ROL|ROS|ROT|ROW|RSL|RÜD|RÜG|SAB|SAD|SAN|SAW|SBG|SBK|SCZ|SDH|SDL|SDT|SEB|SEE|SEF|SEL|SFB|SFT|SGH|SHA|SHG|SHK|SHL|SIG|SIM|SLE|SLF|SLK|SLN|SLS|SLÜ|SLZ|SMÜ|SOB|SOG|SOK|SÖM|SON|SPB|SPN|SRB|SRO|STA|STB|STD|STE|STL|SUL|SÜW|SWA|SZB|TBB|TDO|TET|TIR|TÖL|TUT|UEM|UER|UFF|USI|VAI|VEC|VER|VIB|VIE|VIT|VOH|WAF|WAK|WAN|WAR|WAT|WBS|WDA|WEL|WEN|WER|WES|WHV|WIL|WIS|WIT|WIZ|WLG|WMS|WND|WOB|WOH|WOL|WOR|WOS|WRN|WSF|WST|WSW|WTL|WTM|WUG|WÜM|WUN|WUR|WZL|ZEL|ZIG)[- ]?(([A-Z][- ]?\d{1,4})|([A-Z]{2}[- ]?\d{1,3})))[- ]?(E|H)?$/.test(str), 'de-LI': str => /^FL[- ]?\d{1,5}[UZ]?$/.test(str), + 'en-IN': str => /^[A-Z]{2}[ -]?[0-9]{1,2}(?:[ -]?[A-Z])(?:[ -]?[A-Z]*)?[ -]?[0-9]{4}$/.test(str), + 'es-AR': str => /^(([A-Z]{2} ?[0-9]{3} ?[A-Z]{2})|([A-Z]{3} ?[0-9]{3}))$/.test(str), 'fi-FI': str => /^(?=.{4,7})(([A-Z]{1,3}|[0-9]{1,3})[\s-]?([A-Z]{1,3}|[0-9]{1,5}))$/.test(str), + 'hu-HU': str => /^((((?!AAA)(([A-NPRSTVZWXY]{1})([A-PR-Z]{1})([A-HJ-NPR-Z]))|(A[ABC]I)|A[ABC]O|A[A-W]Q|BPI|BPO|UCO|UDO|XAO)-(?!000)\d{3})|(M\d{6})|((CK|DT|CD|HC|H[ABEFIKLMNPRSTVX]|MA|OT|R[A-Z]) \d{2}-\d{2})|(CD \d{3}-\d{3})|(C-(C|X) \d{4})|(X-(A|B|C) \d{4})|(([EPVZ]-\d{5}))|(S A[A-Z]{2} \d{2})|(SP \d{2}-\d{2}))$/.test(str), + 'pt-BR': str => + /^[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str), 'pt-PT': str => /^([A-Z]{2}|[0-9]{2})[ -·]?([A-Z]{2}|[0-9]{2})[ -·]?([A-Z]{2}|[0-9]{2})$/.test(str), 'sq-AL': str => /^[A-Z]{2}[- ]?((\d{3}[- ]?(([A-Z]{2})|T))|(R[- ]?\d{3}))$/.test(str), - 'pt-BR': str => - /^[A-Z]{3}[ -]?[0-9][A-Z][0-9]{2}|[A-Z]{3}[ -]?[0-9]{4}$/.test(str), 'sv-SE': str => /^[A-HJ-PR-UW-Z]{3} ?[\d]{2}[A-HJ-PR-UW-Z1-9]$|(^[A-ZÅÄÖ ]{2,7}$)/.test(str.trim()), - 'en-IN': str => /^[A-Z]{2}[ -]?[0-9]{1,2}(?:[ -]?[A-Z])(?:[ -]?[A-Z]*)?[ -]?[0-9]{4}$/.test(str), }; -validators['hi-IN'] = validators['en-IN']; -validators['gu-IN'] = validators['en-IN']; -validators['as-IN'] = validators['en-IN']; -validators['bn-IN'] = validators['en-IN']; -validators['kn-IN'] = validators['en-IN']; -validators['ml-IN'] = validators['en-IN']; -validators['mr-IN'] = validators['en-IN']; -validators['or-IN'] = validators['en-IN']; -validators['pa-IN'] = validators['en-IN']; -validators['sa-IN'] = validators['en-IN']; -validators['ta-IN'] = validators['en-IN']; -validators['te-IN'] = validators['en-IN']; -validators['kok-IN'] = validators['en-IN']; - export default function isLicensePlate(str, locale) { assertString(str); if (locale in validators) { diff --git a/src/lib/isLuhnValid.js b/src/lib/isLuhnNumber.js similarity index 93% rename from src/lib/isLuhnValid.js rename to src/lib/isLuhnNumber.js index da205271f..95a066115 100644 --- a/src/lib/isLuhnValid.js +++ b/src/lib/isLuhnNumber.js @@ -1,6 +1,6 @@ import assertString from './util/assertString'; -export default function isLuhnValid(str) { +export default function isLuhnNumber(str) { assertString(str); const sanitized = str.replace(/[- ]+/g, ''); let sum = 0; diff --git a/src/lib/isMimeType.js b/src/lib/isMimeType.js index 1dfa77767..4081117af 100644 --- a/src/lib/isMimeType.js +++ b/src/lib/isMimeType.js @@ -26,7 +26,7 @@ import assertString from './util/assertString'; // NB : // Subtype length must not exceed 100 characters. // This rule does not comply to the RFC specs (what is the max length ?). -const mimeTypeSimple = /^(application|audio|font|image|message|model|multipart|text|video)\/[a-zA-Z0-9\.\-\+]{1,100}$/i; // eslint-disable-line max-len +const mimeTypeSimple = /^(application|audio|font|image|message|model|multipart|text|video)\/[a-zA-Z0-9\.\-\+_]{1,100}$/i; // eslint-disable-line max-len // Handle "charset" in "text/*" const mimeTypeText = /^text\/[a-zA-Z0-9\.\-\+]{1,100};\s?charset=("[a-zA-Z0-9\.\-\+\s]{0,70}"|[a-zA-Z0-9\.\-\+]{0,70})(\s?\([a-zA-Z0-9\.\-\+\s]{1,20}\))?$/i; // eslint-disable-line max-len diff --git a/src/lib/isMobilePhone.js b/src/lib/isMobilePhone.js index f9c552e2f..5c37d42bb 100644 --- a/src/lib/isMobilePhone.js +++ b/src/lib/isMobilePhone.js @@ -10,7 +10,7 @@ const phones = { 'ar-EG': /^((\+?20)|0)?1[0125]\d{8}$/, 'ar-IQ': /^(\+?964|0)?7[0-9]\d{8}$/, 'ar-JO': /^(\+?962|0)?7[789]\d{7}$/, - 'ar-KW': /^(\+?965)[569]\d{7}$/, + 'ar-KW': /^(\+?965)([569]\d{7}|41\d{6})$/, 'ar-LY': /^((\+?218)|0)?(9[1-6]\d{7}|[1-8]\d{7,9})$/, 'ar-MA': /^(?:(?:\+|00)212|0)[5-7]\d{8}$/, 'ar-OM': /^((\+|00)968)?(9[1-9])\d{6}$/, @@ -30,13 +30,13 @@ const phones = { 'de-AT': /^(\+43|0)\d{1,4}\d{3,12}$/, 'de-CH': /^(\+41|0)([1-9])\d{1,9}$/, 'de-LU': /^(\+352)?((6\d1)\d{6})$/, - 'dv-MV': /^(\+?960)?(7[2-9]|91|9[3-9])\d{7}$/, - 'el-GR': /^(\+?30|0)?(69\d{8})$/, + 'dv-MV': /^(\+?960)?(7[2-9]|9[1-9])\d{5}$/, + 'el-GR': /^(\+?30|0)?6(8[5-9]|9(?![26])[0-9])\d{7}$/, 'el-CY': /^(\+?357?)?(9(9|6)\d{6})$/, 'en-AI': /^(\+?1|0)264(?:2(35|92)|4(?:6[1-2]|76|97)|5(?:3[6-9]|8[1-4])|7(?:2(4|9)|72))\d{4}$/, 'en-AU': /^(\+?61|0)4\d{8}$/, 'en-AG': /^(?:\+1|1)268(?:464|7(?:1[3-9]|[28]\d|3[0246]|64|7[0-689]))\d{4}$/, - 'en-BM': /^(\+?1)?441(((3|7)\d{6}$)|(5[0-3][0-9]\d{4}$)|(59\d{5}))/, + 'en-BM': /^(\+?1)?441(((3|7)\d{6}$)|(5[0-3][0-9]\d{4}$)|(59\d{5}$))/, 'en-BS': /^(\+?1[-\s]?|0)?\(?242\)?[-\s]?\d{3}[-\s]?\d{4}$/, 'en-GB': /^(\+?44|0)7\d{9}$/, 'en-GG': /^(\+?44|0)1481\d{6}$/, @@ -48,6 +48,7 @@ const phones = { 'en-IN': /^(\+?91|0)?[6789]\d{9}$/, 'en-JM': /^(\+?876)?\d{7}$/, 'en-KE': /^(\+?254|0)(7|1)\d{8}$/, + 'en-SS': /^(\+?211|0)(9[1257])\d{7}$/, 'en-KI': /^((\+686|686)?)?( )?((6|7)(2|3|8)[0-9]{6})$/, 'en-KN': /^(?:\+1|1)869(?:46\d|48[89]|55[6-8]|66\d|76[02-7])\d{4}$/, 'en-LS': /^(\+?266)(22|28|57|58|59|27|52)\d{6}$/, @@ -76,7 +77,7 @@ const phones = { 'es-CR': /^(\+506)?[2-8]\d{7}$/, 'es-CU': /^(\+53|0053)?5\d{7}/, 'es-DO': /^(\+?1)?8[024]9\d{7}$/, - 'es-HN': /^(\+?504)?[9|8]\d{7}$/, + 'es-HN': /^(\+?504)?[9|8|3|2]\d{7}$/, 'es-EC': /^(\+?593|0)([2-7]|9[2-9])\d{7}$/, 'es-ES': /^(\+?34)?[6|7]\d{8}$/, 'es-PE': /^(\+?51)?9\d{8}$/, @@ -94,6 +95,7 @@ const phones = { 'fo-FO': /^(\+?298)?\s?\d{2}\s?\d{2}\s?\d{2}$/, 'fr-BF': /^(\+226|0)[67]\d{7}$/, 'fr-BJ': /^(\+229)\d{8}$/, + 'fr-CD': /^(\+?243|0)?(8|9)\d{8}$/, 'fr-CM': /^(\+?237)6[0-9]{8}$/, 'fr-FR': /^(\+?33|0)[67]\d{8}$/, 'fr-GF': /^(\+?594|0|00594)[67]\d{8}$/, @@ -118,7 +120,7 @@ const phones = { 'mg-MG': /^((\+?261|0)(2|3)\d)?\d{7}$/, 'mn-MN': /^(\+|00|011)?976(77|81|88|91|94|95|96|99)\d{6}$/, 'my-MM': /^(\+?959|09|9)(2[5-7]|3[1-2]|4[0-5]|6[6-9]|7[5-9]|9[6-9])[0-9]{7}$/, - 'ms-MY': /^(\+?6?01){1}(([0145]{1}(\-|\s)?\d{7,8})|([236789]{1}(\s|\-)?\d{7}))$/, + 'ms-MY': /^(\+?60|0)1(([0145](-|\s)?\d{7,8})|([236-9](-|\s)?\d{7}))$/, 'mz-MZ': /^(\+?258)?8[234567]\d{7}$/, 'nb-NO': /^(\+?47)?[49]\d{7}$/, 'ne-NP': /^(\+?977)?9[78]\d{8}$/, @@ -130,7 +132,8 @@ const phones = { 'pt-BR': /^((\+?55\ ?[1-9]{2}\ ?)|(\+?55\ ?\([1-9]{2}\)\ ?)|(0[1-9]{2}\ ?)|(\([1-9]{2}\)\ ?)|([1-9]{2}\ ?))((\d{4}\-?\d{4})|(9[1-9]{1}\d{3}\-?\d{4}))$/, 'pt-PT': /^(\+?351)?9[1236]\d{7}$/, 'pt-AO': /^(\+244)\d{9}$/, - 'ro-RO': /^(\+?4?0)\s?7\d{2}(\/|\s|\.|\-)?\d{3}(\s|\.|\-)?\d{3}$/, + 'ro-MD': /^(\+?373|0)((6(0|1|2|6|7|8|9))|(7(6|7|8|9)))\d{6}$/, + 'ro-RO': /^(\+?40|0)\s?7\d{2}(\/|\s|\.|-)?\d{3}(\s|\.|-)?\d{3}$/, 'ru-RU': /^(\+?7|8)?9\d{9}$/, 'si-LK': /^(?:0|94|\+94)?(7(0|1|2|4|5|6|7|8)( |-)?)\d{7}$/, 'sl-SI': /^(\+386\s?|0)(\d{1}\s?\d{3}\s?\d{2}\s?\d{2}|\d{2}\s?\d{3}\s?\d{3})$/, diff --git a/src/lib/isPassportNumber.js b/src/lib/isPassportNumber.js index 5b2ebfd7a..11d01e8d1 100644 --- a/src/lib/isPassportNumber.js +++ b/src/lib/isPassportNumber.js @@ -11,6 +11,7 @@ const passportRegexByCountryCode = { AR: /^[A-Z]{3}\d{6}$/, // ARGENTINA AT: /^[A-Z]\d{7}$/, // AUSTRIA AU: /^[A-Z]\d{7}$/, // AUSTRALIA + AZ: /^[A-Z]{2,3}\d{7,8}$/, // AZERBAIJAN BE: /^[A-Z]{2}\d{6}$/, // BELGIUM BG: /^\d{9}$/, // BULGARIA BR: /^[A-Z]{2}\d{6}$/, // BRAZIL @@ -37,8 +38,11 @@ const passportRegexByCountryCode = { IR: /^[A-Z]\d{8}$/, // IRAN IS: /^(A)\d{7}$/, // ICELAND IT: /^[A-Z0-9]{2}\d{7}$/, // ITALY + JM: /^[Aa]\d{7}$/, // JAMAICA JP: /^[A-Z]{2}\d{7}$/, // JAPAN KR: /^[MS]\d{8}$/, // SOUTH KOREA, REPUBLIC OF KOREA, [S=PS Passports, M=PM Passports] + KZ: /^[a-zA-Z]\d{7}$/, // KAZAKHSTAN + LI: /^[a-zA-Z]\d{5}$/, // LIECHTENSTEIN LT: /^[A-Z0-9]{8}$/, // LITHUANIA LU: /^[A-Z0-9]{8}$/, // LUXEMBURG LV: /^[A-Z0-9]{2}\d{7}$/, // LATVIA @@ -48,12 +52,15 @@ const passportRegexByCountryCode = { MY: /^[AHK]\d{8}$/, // MALAYSIA MX: /^\d{10,11}$/, // MEXICO NL: /^[A-Z]{2}[A-Z0-9]{6}\d$/, // NETHERLANDS + NZ: /^([Ll]([Aa]|[Dd]|[Ff]|[Hh])|[Ee]([Aa]|[Pp])|[Nn])\d{6}$/, // NEW ZEALAND + PH: /^([A-Z](\d{6}|\d{7}[A-Z]))|([A-Z]{2}(\d{6}|\d{7}))$/, // PHILIPPINES + PK: /^[A-Z]{2}\d{7}$/, // PAKISTAN PL: /^[A-Z]{2}\d{7}$/, // POLAND PT: /^[A-Z]\d{6}$/, // PORTUGAL RO: /^\d{8,9}$/, // ROMANIA RU: /^\d{9}$/, // RUSSIAN FEDERATION SE: /^\d{8}$/, // SWEDEN - SL: /^(P)[A-Z]\d{7}$/, // SLOVANIA + SL: /^(P)[A-Z]\d{7}$/, // SLOVENIA SK: /^[0-9A-Z]\d{7}$/, // SLOVAKIA TH: /^[A-Z]{1,2}\d{6,7}$/, // THAILAND TR: /^[A-Z]\d{8}$/, // TURKEY diff --git a/src/lib/isPostalCode.js b/src/lib/isPostalCode.js index cf5b50d25..e6213914f 100644 --- a/src/lib/isPostalCode.js +++ b/src/lib/isPostalCode.js @@ -15,7 +15,7 @@ const patterns = { BE: fourDigit, BG: fourDigit, BR: /^\d{5}-\d{3}$/, - BY: /2[1-4]{1}\d{4}$/, + BY: /^2[1-4]\d{4}$/, CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i, CH: fourDigit, CN: /^(0[1-7]|1[012356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[1-5]|8[1345]|9[09])\d{4}$/, @@ -37,7 +37,7 @@ const patterns = { IE: /^(?!.*(?:o))[A-Za-z]\d[\dw]\s\w{4}$/i, IL: /^(\d{5}|\d{7})$/, IN: /^((?!10|29|35|54|55|65|66|86|87|88|89)[1-9][0-9]{5})$/, - IR: /\b(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}\b/, + IR: /^(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}$/, IS: threeDigit, IT: fiveDigit, JP: /^\d{3}\-\d{4}$/, diff --git a/src/lib/isRgbColor.js b/src/lib/isRgbColor.js index e6508e29a..9458522ab 100644 --- a/src/lib/isRgbColor.js +++ b/src/lib/isRgbColor.js @@ -2,8 +2,8 @@ import assertString from './util/assertString'; const rgbColor = /^rgb\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){2}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\)$/; const rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)$/; -const rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]%|100%)\)/; -const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)/; +const rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]%|100%)\)$/; +const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)$/; export default function isRgbColor(str, includePercentValues = true) { assertString(str); diff --git a/src/lib/isStrongPassword.js b/src/lib/isStrongPassword.js index 28bb0637f..5db901fa3 100644 --- a/src/lib/isStrongPassword.js +++ b/src/lib/isStrongPassword.js @@ -4,7 +4,7 @@ import assertString from './util/assertString'; const upperCaseRegex = /^[A-Z]$/; const lowerCaseRegex = /^[a-z]$/; const numberRegex = /^[0-9]$/; -const symbolRegex = /^[-#!$@%^&*()_+|~=`{}\[\]:";'<>?,.\/ ]$/; +const symbolRegex = /^[-#!$@£%^&*()_+|~=`{}\[\]:";'<>?,.\/ ]$/; const defaultOptions = { minLength: 8, diff --git a/src/lib/isTime.js b/src/lib/isTime.js new file mode 100644 index 000000000..076bbfa34 --- /dev/null +++ b/src/lib/isTime.js @@ -0,0 +1,23 @@ +import merge from './util/merge'; + +const default_time_options = { + hourFormat: 'hour24', + mode: 'default', +}; + +const formats = { + hour24: { + default: /^([01]?[0-9]|2[0-3]):([0-5][0-9])$/, + withSeconds: /^([01]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/, + }, + hour12: { + default: /^(0?[1-9]|1[0-2]):([0-5][0-9]) (A|P)M$/, + withSeconds: /^(0?[1-9]|1[0-2]):([0-5][0-9]):([0-5][0-9]) (A|P)M$/, + }, +}; + +export default function isTime(input, options) { + options = merge(options, default_time_options); + if (typeof input !== 'string') return false; + return formats[options.hourFormat][options.mode].test(input); +} diff --git a/src/lib/toDate.js b/src/lib/toDate.js index 7cf80510e..179645fda 100644 --- a/src/lib/toDate.js +++ b/src/lib/toDate.js @@ -2,6 +2,7 @@ import assertString from './util/assertString'; export default function toDate(date) { assertString(date); + date = Date.parse(date); return !isNaN(date) ? new Date(date) : null; }