Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for zh-HK in isIdentityCard #2142

Merged
merged 10 commits into from
Jan 20, 2023
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).<br/><br/>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.<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'.
**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'.
**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.
**isIn(str, values)** | check if the string is in a array of allowed values.
**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).
Expand Down
31 changes: 31 additions & 0 deletions src/lib/isIdentityCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down