Files
modstagram-next/src/lib/iranNationalCode.ts
payacom 5ecbcd1f26 shop
2026-07-31 21:20:13 +03:30

16 lines
550 B
TypeScript

/** Standard Iranian national-code (کد ملی) checksum validator. */
export function isValidIranNationalCode(code: string): boolean {
const value = String(code || "");
if (!/^\d{10}$/.test(value)) return false;
if (/^(\d)\1{9}$/.test(value)) return false;
const digits = value.split("").map(Number);
const sum = digits
.slice(0, 9)
.reduce((acc, digit, index) => acc + digit * (10 - index), 0);
const remainder = sum % 11;
const checkDigit = remainder < 2 ? remainder : 11 - remainder;
return checkDigit === digits[9];
}