From c19777abf4ad6a3b422872a8da3a5549d3c2fccc Mon Sep 17 00:00:00 2001 From: Songyue Wang <98693645+songyuew@users.noreply.github.com> Date: Fri, 20 Jan 2023 14:10:01 +0800 Subject: [PATCH] feat(isIdentityCard): add hk-HK locale (#2142) --- README.md | 2 +- src/lib/isIdentityCard.js | 31 +++++++++++++++++++++++++++++++ test/validators.js | 23 +++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9372230e7..42e7c234a 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,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 `['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'. +**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 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 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/test/validators.js b/test/validators.js index c0e36bec7..d270f0f2b 100644 --- a/test/validators.js +++ b/test/validators.js @@ -5518,6 +5518,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: [