diff --git a/README.md b/README.md index f930719a8..514c4c0d3 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ Validator | Description **isOctal(str)** | check if the string is a valid octal number. **isPassportNumber(str, countryCode)** | check if the string is a valid passport number relative to a specific country code. **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', 'BE', 'BG', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MT', 'MX', 'NL', 'NO', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', '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`.). +**isPostalCode(str, locale)** | check if the string is a postal code,

(locale is one of `[ 'AD', 'AT', 'AU', 'BE', 'BG', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MT', 'MX', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SI', '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`.). **isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer). **isSurrogatePair(str)** | check if the string contains any surrogate pairs chars. **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_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, disallow_auth: false }`.

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.
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed. diff --git a/es/lib/isPostalCode.js b/es/lib/isPostalCode.js index a33300724..0e0cf7ec0 100644 --- a/es/lib/isPostalCode.js +++ b/es/lib/isPostalCode.js @@ -41,6 +41,7 @@ var patterns = { MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/, NL: /^\d{4}\s?[a-z]{2}$/i, NO: fourDigit, + NP: /^(10|21|22|32|33|34|44|45|56|57)\d{3}$|^(977)$/i, NZ: fourDigit, PL: /^\d{2}\-\d{3}$/, PR: /^00[679]\d{2}([ -]\d{4})?$/, @@ -59,7 +60,7 @@ var patterns = { ZM: fiveDigit }; export var locales = Object.keys(patterns); -export default function (str, locale) { +export default function isPostalCode(str, locale) { assertString(str); if (locale in patterns) { diff --git a/lib/isPostalCode.js b/lib/isPostalCode.js index 7dab73f7b..333152fba 100644 --- a/lib/isPostalCode.js +++ b/lib/isPostalCode.js @@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); -exports.default = _default; +exports.default = isPostalCode; exports.locales = void 0; var _assertString = _interopRequireDefault(require("./util/assertString")); @@ -52,6 +52,7 @@ var patterns = { MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/, NL: /^\d{4}\s?[a-z]{2}$/i, NO: fourDigit, + NP: /^(10|21|22|32|33|34|44|45|56|57)\d{3}$|^(977)$/i, NZ: fourDigit, PL: /^\d{2}\-\d{3}$/, PR: /^00[679]\d{2}([ -]\d{4})?$/, @@ -72,7 +73,7 @@ var patterns = { var locales = Object.keys(patterns); exports.locales = locales; -function _default(str, locale) { +function isPostalCode(str, locale) { (0, _assertString.default)(str); if (locale in patterns) { diff --git a/src/lib/isPostalCode.js b/src/lib/isPostalCode.js index 4800b3cea..fdb537de4 100644 --- a/src/lib/isPostalCode.js +++ b/src/lib/isPostalCode.js @@ -43,6 +43,7 @@ const patterns = { MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/, NL: /^\d{4}\s?[a-z]{2}$/i, NO: fourDigit, + NP: /^(10|21|22|32|33|34|44|45|56|57)\d{3}$|^(977)$/i, NZ: fourDigit, PL: /^\d{2}\-\d{3}$/, PR: /^00[679]\d{2}([ -]\d{4})?$/, diff --git a/test/validators.js b/test/validators.js index 6dd77f8f0..22b2927cd 100755 --- a/test/validators.js +++ b/test/validators.js @@ -7962,6 +7962,22 @@ describe('Validators', () => { '3997 GH', ], }, + { + locale: 'NP', + valid: [ + '10811', + '32600', + '56806', + '977', + ], + invalid: [ + '11977', + 'asds', + '13 32', + '-977', + '97765', + ], + }, { locale: 'PL', valid: [