Function
Algorithm
- Falsy input →
undefined. - Stringify; length must be ≥ 11.
- Reject if
parseInt(legalId) === 0. - Reject if the middle 6 digits (
slice(3, 9)) are all zero. - Compute check digit with coefficient vector
[29, 27, 23, 19, 17](cycled per period of 5):
Pitfalls
- Length check is
< 11, not=== 11. Inputs longer than 11 chars are accepted; the algorithm slices fixed indices, so longer strings can still validate. If you need strict 11-digit input, checkString(input).length === 11separately. - Persian/Arabic digit input is not normalized.
parseInt("۱۰۳۸۰۲۸۴۷۹۰")isNaN. RunautoConvertDigitsToENfirst. - Return type is
boolean | undefined— use=== true, not truthy. - For personal IDs (10 digits, different algorithm), use
verifyIranianNationalId.
Source
src/modules/legalId/index.ts · Tests: test/verifyIranianLegalId.spec.ts