Skip to content

Commit c19777a

Browse files
authored
feat(isIdentityCard): add hk-HK locale (#2142)
1 parent 93667bd commit c19777a

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ Validator | Description
121121
**isHexColor(str)** | check if the string is a hexadecimal color.
122122
**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).<br/><br/>Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`).
123123
**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number).
124-
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`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.<br/><br/>Defaults to 'any'.
124+
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`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.<br/><br/>Defaults to 'any'.
125125
**isIMEI(str [, options]))** | check if the string is a valid IMEI number. Imei should be of format `###############` or `##-######-######-#`.<br/><br/>`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.
126126
**isIn(str, values)** | check if the string is in a array of allowed values.
127127
**isInt(str [, options])** | check if the string is an integer.<br/><br/>`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).

src/lib/isIdentityCard.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,37 @@ const validators = {
342342
};
343343
return checkIdCardNo(str);
344344
},
345+
'zh-HK': (str) => {
346+
// sanitize user input
347+
str = str.trim();
348+
349+
// HKID number starts with 1 or 2 letters, followed by 6 digits,
350+
// then a checksum contained in square / round brackets or nothing
351+
const regexHKID = /^[A-Z]{1,2}[0-9]{6}((\([0-9A]\))|(\[[0-9A]\])|([0-9A]))$/;
352+
const regexIsDigit = /^[0-9]$/;
353+
354+
// convert the user input to all uppercase and apply regex
355+
str = str.toUpperCase();
356+
if (!regexHKID.test(str)) return false;
357+
str = str.replace(/\[|\]|\(|\)/g, '');
358+
359+
if (str.length === 8) str = `3${str}`;
360+
let checkSumVal = 0;
361+
for (let i = 0; i <= 7; i++) {
362+
let convertedChar;
363+
if (!regexIsDigit.test(str[i])) convertedChar = (str[i].charCodeAt(0) - 55) % 11;
364+
else convertedChar = str[i];
365+
checkSumVal += (convertedChar * (9 - i));
366+
}
367+
checkSumVal %= 11;
368+
369+
let checkSumConverted;
370+
if (checkSumVal === 0) checkSumConverted = '0';
371+
else if (checkSumVal === 1) checkSumConverted = 'A';
372+
else checkSumConverted = String(11 - checkSumVal);
373+
if (checkSumConverted === str[str.length - 1]) return true;
374+
return false;
375+
},
345376
'zh-TW': (str) => {
346377
const ALPHABET_CODES = {
347378
A: 10,

test/validators.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5518,6 +5518,29 @@ describe('Validators', () => {
55185518

55195519
it('should validate identity cards', () => {
55205520
const fixtures = [
5521+
{
5522+
locale: 'zh-HK',
5523+
valid: [
5524+
'OV290326[A]',
5525+
'Q803337[0]',
5526+
'Z0977986',
5527+
'W520128(7)',
5528+
'A494866[4]',
5529+
'A494866(4)',
5530+
'Z867821A',
5531+
'ag293013(9)',
5532+
'k348609(5)',
5533+
],
5534+
invalid: [
5535+
'A1234567890',
5536+
'98765432',
5537+
'O962472(9)',
5538+
'M4578601',
5539+
'X731324[8]',
5540+
'C503134(5)',
5541+
'RH265886(3)',
5542+
],
5543+
},
55215544
{
55225545
locale: 'LK',
55235546
valid: [

0 commit comments

Comments
 (0)