Skip to content

Commit 39830a9

Browse files
mhf-irfedeci
andauthored
feat: IR passport and identityCard, respect .gitignore files (#1595)
* feat: isLuhn, IR passport and identityCard, respect .gitignore files * Update test/validators.js Co-authored-by: Federico Ciardi <[email protected]> * fix: rEADME.md * fix: remove isLuhn Co-authored-by: Federico Ciardi <[email protected]>
1 parent 5d6db63 commit 39830a9

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Validator | Description
115115
**isHexColor(str)** | check if the string is a hexadecimal color.
116116
**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)`).
117117
**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number).
118-
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'IN', 'IT', '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'.
118+
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'IN', 'IT', 'IR', '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'.
119119
**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.
120120
**isIn(str, values)** | check if the string is in a array of allowed values.
121121
**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).
@@ -144,7 +144,7 @@ Validator | Description
144144
**isMultibyte(str)** | check if the string contains one or more multibyte chars.
145145
**isNumeric(str [, options])** | check if the string contains only numbers.<br/><br/>`options` is an object which defaults to `{no_symbols: false}` it also has locale as an option. If `no_symbols` is true, the validator will reject numeric strings that feature a symbol (e.g. `+`, `-`, or `.`).<br/><br/>`locale` determine the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-FR', 'fr-CA', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`.
146146
**isOctal(str)** | check if the string is a valid octal number.
147-
**isPassportNumber(str, countryCode)** | check if the string is a valid passport number.<br/><br/>(countryCode is one of `[ 'AM', 'AR', 'AT', 'AU', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE' 'IN', 'IS', 'IT', 'JP', 'KR', 'LT', 'LU', 'LV', 'LY', 'MT', 'MY', 'NL', 'PO', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TR', 'UA', 'US' ]`.
147+
**isPassportNumber(str, countryCode)** | check if the string is a valid passport number.<br/><br/>(countryCode is one of `[ 'AM', 'AR', 'AT', 'AU', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE' 'IN', 'IR', 'IS', 'IT', 'JP', 'KR', 'LT', 'LU', 'LV', 'LY', 'MT', 'MY', 'NL', 'PO', 'PT', 'RO', 'RU', 'SE', 'SL', 'SK', 'TR', 'UA', 'US' ]`.
148148
**isPort(str)** | check if the string is a valid port number.
149149
**isPostalCode(str, locale)** | check if the string is a postal code,<br/><br/>(locale is one of `[ 'AD', 'AT', 'AU', 'AZ', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HT', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'LI', 'LT', 'LU', 'LV', 'MT', 'MX', 'MY', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SG', 'SI', 'TH', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.).
150150
**isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date.

src/lib/isIdentityCard.js

+19
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,25 @@ const validators = {
7575

7676
return c === 0;
7777
},
78+
IR: (str) => {
79+
if (!str.match(/^\d{10}$/)) return false;
80+
str = (`0000${str}`).substr(str.length - 6);
81+
82+
if (parseInt(str.substr(3, 6), 10) === 0) return false;
83+
84+
const lastNumber = parseInt(str.substr(9, 1), 10);
85+
let sum = 0;
86+
87+
for (let i = 0; i < 9; i++) {
88+
sum += parseInt(str.substr(i, 1), 10) * (10 - i);
89+
}
90+
91+
sum %= 11;
92+
93+
return (
94+
(sum < 2 && lastNumber === sum) || (sum >= 2 && lastNumber === 11 - sum)
95+
);
96+
},
7897
IT: function IT(str) {
7998
if (str.length !== 9) return false;
8099
if (str === 'CA00000AA') return false; // https://it.wikipedia.org/wiki/Carta_d%27identit%C3%A0_elettronica_italiana

src/lib/isPassportNumber.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const passportRegexByCountryCode = {
3333
HU: /^[A-Z]{2}(\d{6}|\d{7})$/, // HUNGARY
3434
IE: /^[A-Z0-9]{2}\d{7}$/, // IRELAND
3535
IN: /^[A-Z]{1}-?\d{7}$/, // INDIA
36+
IR: /^[A-Z]\d{8}$/, // IRAN
3637
IS: /^(A)\d{7}$/, // ICELAND
3738
IT: /^[A-Z0-9]{2}\d{7}$/, // ITALY
3839
JP: /^[A-Z]{2}\d{7}$/, // JAPAN

test/validators.js

+40
Original file line numberDiff line numberDiff line change
@@ -2623,6 +2623,21 @@ describe('Validators', () => {
26232623
],
26242624
});
26252625

2626+
test({
2627+
validator: 'isPassportNumber',
2628+
args: ['IR'],
2629+
valid: [
2630+
'J97634522',
2631+
'A01234567',
2632+
'Z11977831',
2633+
],
2634+
invalid: [
2635+
'A0123456',
2636+
'A0123456Z',
2637+
'012345678',
2638+
],
2639+
});
2640+
26262641
test({
26272642
validator: 'isPassportNumber',
26282643
args: ['IS'],
@@ -4545,6 +4560,31 @@ describe('Validators', () => {
45454560
'X1234567L',
45464561
],
45474562
},
4563+
{
4564+
locale: 'IR',
4565+
valid: [
4566+
'0499370899',
4567+
'0790419904',
4568+
'0084575948',
4569+
'0963695398',
4570+
'0684159414',
4571+
'0067749828',
4572+
'0650451252',
4573+
'1583250689',
4574+
'4032152314',
4575+
'0076229645',
4576+
'4271467685',
4577+
'0200203241',
4578+
],
4579+
invalid: [
4580+
'1260293040',
4581+
'0000000001',
4582+
'1999999999',
4583+
'9999999991',
4584+
'AAAAAAAAAA',
4585+
'0684159415',
4586+
],
4587+
},
45484588
{
45494589
locale: 'IT',
45504590
valid: [

0 commit comments

Comments
 (0)