Mod-97 Check Digit Calculation and Validation
Compute ISO 7064 mod-97 check digits and validate long number strings without exceeding JavaScript safe integer precision.
Code
export function mod97(input) {
const digits = String(input).replace(/[\s-]/g, "");
if (!/^\d+$/.test(digits)) throw new TypeError("input must contain digits only");
let remainder = 0;
for (const char of digits) {
remainder = (remainder * 10 + Number(char)) % 97;
}
return remainder;
}
export function computeCheckDigits(base) {
const digits = String(base).replace(/[\s-]/g, "");
if (!/^\d+$/.test(digits)) throw new TypeError("base must contain digits only");
const check = 98 - mod97(digits + "00");
return String(check).padStart(2, "0");
}
export function validateCheckDigits(value) {
const digits = String(value).replace(/[\s-]/g, "");
if (!/^\d+$/.test(digits)) throw new TypeError("value must contain digits only");
return mod97(digits) === 1;
}Report a problem with this exampleOpens a pre-filled issue on GitHub.