Skip to content
Open
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,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][CSS Colors Level 4 Specification].<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, [, options])** | check if the string is an IBAN (International Bank Account Number).<br/><br/>`options` is an object which accepts two attributes: `whitelist`: where you can restrict IBAN codes you want to receive data from and `blacklist`: where you can remove some of the countries from the current list. For both you can use an array with the following values `['AD','AE','AL','AT','AZ','BA','BE','BG','BH','BR','BY','CH','CR','CY','CZ','DE','DK','DO','EE','EG','ES','FI','FO','FR','GB','GE','GI','GL','GR','GT','HR','HU','IE','IL','IQ','IR','IS','IT','JO','KW','KZ','LB','LC','LI','LT','LU','LV','MC','MD','ME','MK','MR','MT','MU','MZ','NL','NO','PK','PL','PS','PT','QA','RO','RS','SA','SC','SE','SI','SK','SM','SV','TL','TN','TR','UA','VA','VG','XK']`.
**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', 'PK']` OR `'any'`. If 'any' is used, function will check if any of the locales 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 `['KR', 'LK', 'PL', 'ES', 'FI', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN', 'zh-HK', 'PK']` OR `'any'`. If 'any' is used, function will check if any of the locales match.<br/><br/>Defaults to 'any'.
**isIMEI(str [, options]))** | check if the string is a valid [IMEI number][IMEI]. 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 an 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
29 changes: 29 additions & 0 deletions src/lib/isIdentityCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@ import includes from './util/includesArray';
import isInt from './isInt';

const validators = {
KR: function KR(str) {
// {birthday 6 digits} + '-' + {gender 1 digit} + {random 6 digits}
const birthdayRegex = /([0-9][0-9])(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])/;
const genderRegex = /([1-4])/;
const randomRegex = /(\d{6})/;
const totalRegex = new RegExp(`^${birthdayRegex.source}-${genderRegex.source}${randomRegex.source}$`);

if (!totalRegex.test(str)) {
return false;
}

// Before 2000 gender digit should be 1,2
// After 2000 gender digit should be 3,4
// ex) current time: 2025-01-01 --> 990101-3 can't be exist because it means born in 2099-01-01.
// const now = new Date().toLocaleString('ko-KR', { timeZone: 'Asia/Seoul' });
// const currentYearDigits = Number(now.slice(2, 4));
// const yearDigits = Number(str.slice(0, 2));
// const genderDigit = Number(str.slice(7, 8));

// if (currentYearDigits < yearDigits) {
// if (genderDigit === 3 || genderDigit === 4) {
// return false;
// }
// } else if (genderDigit === 1 || genderDigit === 2) {
// return false;
// }

return true;
},
PL: (str) => {
assertString(str);

Expand Down
21 changes: 21 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6306,6 +6306,27 @@ describe('Validators', () => {

it('should validate identity cards', () => {
const fixtures = [
{
locale: 'KR',
valid: [
'731127-2121311',
'900115-1234567',
'851230-2234567',
'020509-3234567',
'101121-4234567',
'770308-1034567',
],
invalid: [
'991331-1234567',
'900115-5234567',
// '020509-2234567',
'851230-3',
'abcd12-1234567',
'901215_1234567',
'850230-123456',
// '990101-3111111',
],
},
{
locale: 'PK',
valid: [
Expand Down