16 lines
550 B
TypeScript
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];
|
|
}
|