The Iranian National ID (کد ملی) is a 10-digit code with a check digit and an embedded 3-digit city/registration prefix. Persian Tools provides validation, prefix lookup, and a generator family for tests.
Validate
verifyIranianNationalId(nationalId, options?)
Omit the second argument entirely to get the default, which leaves the prefix check off. There is no partial
options object: verifyIranianNationalId(id, {}) is a type error.
The prefix check is off by default as of v5 — it used to be on. The mod-11 check digit is the standard and always
runs; the 3-digit city-code list is community-maintained data that lags behind the codes سازمان ثبت احوال actually
issues, so enabling it will reject genuine IDs carrying a newer prefix.
Algorithm
- Falsy →
undefined.
- Length must be ≥ 8; shorter inputs are zero-padded to 10 (so
499370899 is accepted as 0499370899).
- Reject all-same-digit sequences (
0000000000, 1111111111, …) via the exported invalidNationalIdSequences set.
- If
checkPrefix === true, the leading 3 digits must be in validNationalIdPrefixes.
- Standard check-digit computation:
sum = Σ digit[i] * (10 - i) for i = 0..8, then digit[9] must equal sum % 11 (or 11 - sum%11 if sum%11 ≥ 2).
Look up the place
For city/province from a National ID’s 3-digit prefix, see the Place by National ID page.
Generate (for tests)
Pitfalls
- Persian/Arabic digit input is NOT auto-normalized.
verifyIranianNationalId("۰۴۹۹۳۷۰۸۹۹") returns false because parseInt of Persian digits yields NaN. Run autoConvertDigitsToEN first.
- Return type is
boolean | undefined — use === true, not truthy checks.
- For test fixtures, generate IDs with
createIranianNationalId(...). Don’t commit real personal data.
Source
src/modules/nationalId/index.ts, src/modules/nationalId/create-national-id.ts · Tests: test/verifyIranianNationalId.spec.ts