diff --git a/README.md b/README.md index 366036d43..8a2693f58 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,7 @@ Validator | Description **isMultibyte(str)** | check if the string contains one or more multibyte chars. **isNumeric(str [, options])** | check if the string contains only numbers.

`options` is an object which defaults to `{ no_symbols: false }` it also has `locale` as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).

`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fr-FR', 'fr-CA', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. **isOctal(str)** | check if the string is a valid octal number. -**isPassportNumber(str, countryCode)** | check if the string is a valid passport number.

`countryCode` is one of `['AM', 'AR', 'AT', 'AU', 'AZ', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE', 'IN', 'IR', 'ID', 'IS', 'IT', 'JM', 'JP', 'KR', 'KZ', 'LI', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'NZ', 'PH', 'PK', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TH', 'TR', 'UA', 'US', 'ZA']`. Locale list is `validator.passportNumberLocales`. +**isPassportNumber(str, countryCode)** | check if the string is a valid passport number.

`countryCode` is one of `['AM', 'AR', 'AT', 'AU', 'AZ', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE', 'IN', 'IR', 'ID', 'IS', 'IT', 'JM', 'JP', 'KR', 'KZ', 'LI', 'LT', 'LU', 'LV', 'LY', 'MT', 'MX', 'MY', 'MZ', 'NL', 'NZ', 'PH', 'PK', 'PL', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TH', 'TR', 'UA', 'US', 'ZA']` OR `'any'`. If 'any' is used, function will check if any of the locales match. Locale list is `validator.passportNumberLocales`. **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', 'BD', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CO', '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', 'PK', '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. diff --git a/src/lib/isPassportNumber.js b/src/lib/isPassportNumber.js index c3b842e59..d89549171 100644 --- a/src/lib/isPassportNumber.js +++ b/src/lib/isPassportNumber.js @@ -84,6 +84,18 @@ export default function isPassportNumber(str, countryCode) { /** Remove All Whitespaces, Convert to UPPERCASE */ const normalizedStr = str.replace(/\s/g, '').toUpperCase(); + if (countryCode === 'any') { + for (const key in passportRegexByCountryCode) { + if (passportRegexByCountryCode.hasOwnProperty(key)) { + const regex = passportRegexByCountryCode[key]; + if (regex.test(normalizedStr)) { + return true; + } + } + } + return false; + } + return (countryCode.toUpperCase() in passportRegexByCountryCode) && passportRegexByCountryCode[countryCode].test(normalizedStr); } diff --git a/test/validators.test.js b/test/validators.test.js index 299af27d8..90656e071 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -3641,6 +3641,94 @@ describe('Validators', () => { 'Z12345678', ], }); + + test({ + validator: 'isPassportNumber', + args: ['any'], + valid: [ + 'AB1234567', + 'ABC123456', + 'A1234567', + 'A1234567', + 'A12345678', + 'AB123456', + '123456789', + 'AB123456', + 'AB1234567', + 'AB123456', + 'A1234567', + 'G12345678', + 'EA1234567', + 'A123456', + '12345678', + 'C12345678', + '123456789', + '123456789', + 'K1234567', + 'AB1234567', + 'AB123456', + 'AB1234567', + '12AB12345', + '123456789', + 'AB1234567', + '123456789', + 'A-1234567', + 'A1234567', + 'A12345678', + 'A1234567', + 'AB1234567', + 'A1234567', + 'AB1234567', + 'S12345678', + 'M12345678', + 'A1234567', + 'A12345', + 'AB123456', + 'AB123456', + 'AB1234567', + 'AB123456', + '1234567', + 'AB1234567', + 'A12345678', + '12345678901', + 'AB1234567', + 'LA123456', + 'LD123456', + 'LF123456', + 'LH123456', + 'EA123456', + 'EP123456', + 'N123456', + 'A123456', + 'A1234567A', + 'AB123456', + 'AB1234567', + 'AB1234567', + 'AB1234567', + 'A123456', + '12345678', + '123456789', + '123456789', + '12345678', + 'PA1234567', + 'A1234567', + 'A123456', + 'AB1234567', + 'A12345678', + 'AB123456', + '123456789', + 'A12345678', + 'T12345678', + 'A12345678', + 'M12345678', + 'D12345678', + ], + invalid: [ + '12345', + 'ABCDEFGH!', + 'A1B2C3D4E5F', + ], + }); }); it('should validate decimal numbers', () => {