diff --git a/README.md b/README.md index cb278ff16..044c8c354 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ Validator | Description **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 `['ES', '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'. +**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['PL', 'ES', '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. **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). diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 64616340b..8ea15e262 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -1,6 +1,39 @@ import assertString from './util/assertString'; +import isInt from './isInt'; const validators = { + PL: (str) => { + assertString(str); + + const weightOfDigits = { + 1: 1, + 2: 3, + 3: 7, + 4: 9, + 5: 1, + 6: 3, + 7: 7, + 8: 9, + 9: 1, + 10: 3, + 11: 0, + }; + + if (str != null && str.length === 11 && isInt(str, { allow_leading_zeroes: true })) { + const digits = str.split('').slice(0, -1); + const sum = digits.reduce((acc, digit, index) => + acc + (Number(digit) * weightOfDigits[index + 1]), 0); + + const modulo = sum % 10; + const lastDigit = Number(str.charAt(str.length - 1)); + + if ((modulo === 0 && lastDigit === 0) || lastDigit === 10 - modulo) { + return true; + } + } + + return false; + }, ES: (str) => { assertString(str); diff --git a/test/validators.js b/test/validators.js index 13e066741..286eb6924 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4672,6 +4672,31 @@ describe('Validators', () => { it('should validate identity cards', () => { const fixtures = [ + { + locale: 'PL', + valid: [ + '99012229019', + '09210215408', + '20313034701', + '86051575214', + '77334586883', + '54007481320', + '06566860643', + '77552478861', + ], + invalid: [ + 'aa', + '5', + '195', + '', + ' ', + '12345678901', + '99212229019', + '09210215402', + '20313534701', + '86241579214', + ], + }, { locale: 'ES', valid: [