Skip to content

Commit

Permalink
feat(isIdentityCard): add TH (Thai) (#1657)
Browse files Browse the repository at this point in the history
  • Loading branch information
tithanayut committed Jul 16, 2021
1 parent d36f79c commit f5f4fcd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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).<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 `['ES', 'IN', 'IT', 'IR', 'MZ', 'NO', '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 `['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.<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
10 changes: 10 additions & 0 deletions src/lib/isIdentityCard.js
Expand Up @@ -117,6 +117,16 @@ const validators = {
if (k1 !== f[9] || k2 !== f[10]) return false;
return true;
},
TH: (str) => {
if (!str.match(/^[1-8]\d{12}$/)) return false;

// validate check digit
let sum = 0;
for (let i = 0; i < 12; i++) {
sum += parseInt(str[i], 10) * (13 - i);
}
return str[12] === ((11 - (sum % 11)) % 10).toString();
},
'he-IL': (str) => {
const DNI = /^\d{9}$/;

Expand Down
18 changes: 18 additions & 0 deletions test/validators.js
Expand Up @@ -4690,6 +4690,24 @@ describe('Validators', () => {
'92031470790',
],
},
{
locale: 'TH',
valid: [
'1101230000001',
'1101230000060',
],
invalid: [
'abc',
'1101230',
'11012300000011',
'aaaaaaaaaaaaa',
'110123abcd001',
'1101230000007',
'0101123450000',
'0101123450004',
'9101123450008',
],
},
{
locale: 'he-IL',
valid: [
Expand Down

0 comments on commit f5f4fcd

Please sign in to comment.