How IBAN Modulo 97 and Luhn Checksums Prevent Payment Errors
Understand the math behind Luhn Modulo 10 and ISO 7064 Modulo 97-10 algorithms that validate credit cards and international bank account numbers.
Payment forms and banking systems can catch many typing errors in card and account numbers before anything is sent to a bank, using simple checksum algorithms. Credit cards (Visa, Mastercard, Amex) verify structural integrity via the Luhn Modulo 10 algorithm, while International Bank Account Numbers (IBAN) rely on the ISO 7064 Modulo 97-10 standard. These checks catch all single-digit typos and most transposition errors without querying any banking network.
The Luhn algorithm (Modulo 10) for payment cards
Devised by IBM scientist Hans Peter Luhn (patent filed in 1954, granted in 1960), the Luhn algorithm (specified in ISO/IEC 7812-1) is a simple checksum formula used across credit cards, IMEI device numbers, and national identifiers.
Step-by-step Luhn validation:
- Start from the rightmost digit (the check digit) and move leftward.
- Double the value of every second digit (moving right to left).
- If doubling a digit results in a two-digit number (e.g.,
7 * 2 = 14), sum the digits of that product (e.g.,1 + 4 = 5) or subtract 9 (14 - 9 = 5). - Sum all digits together (both untouched and processed values).
- If the total modulo 10 equals 0, the card number possesses a mathematically valid checksum.
// Complete Luhn validation function in JavaScript
function validateLuhn(cardNumber) {
const sanitized = cardNumber.replace(/\D/g, '');
let sum = 0;
let shouldDouble = false;
for (let i = sanitized.length - 1; i >= 0; i--) {
let digit = parseInt(sanitized.charAt(i), 10);
if (shouldDouble) {
digit *= 2;
if (digit > 9) digit -= 9;
}
sum += digit;
shouldDouble = !shouldDouble;
}
return (sum % 10) === 0;
}
console.log(validateLuhn("4532015112830366")); // Returns true (valid test card)
The ISO 7064 Modulo 97-10 algorithm for IBAN
The International Bank Account Number (IBAN), standardized under ISO 13616, uses a significantly stronger mathematical verification standard: MOD 97-10. An IBAN consists of up to 34 alphanumeric characters structured as:
- Country Code (2 letters, e.g.,
GB,DE,PL) - Check Digits (2 digits)
- Basic Bank Account Number (BBAN, up to 30 characters containing bank branch and account IDs)
| Validation Metric | Luhn Modulo 10 | ISO 7064 Modulo 97-10 |
|---|---|---|
| Primary Implementation | Credit/Debit Cards (Visa, MC, Amex) | Bank Account Numbers (IBAN) |
| Check Digit Size | 1 digit | 2 digits |
| Single Digit Error Detection | 100 percent | 100 percent |
| Adjacent Transposition Detection | About 98 percent (misses only 09 <-> 90) | 100 percent for adjacent digits |
| Modulo Base | 10 | 97 |
Mathematical execution of IBAN validation:
- Move the first four characters (country code and two check digits) to the end of the string.
- Convert all letters to numeric values:
A=10, B=11, C=12 ... Z=35. - Interpret the resulting alphanumeric string as a single massive integer.
- Calculate the remainder of this integer divided by 97.
- If the remainder equals exactly 1, the IBAN structure and check digits are valid.
Handling arbitrary precision math in JavaScript
Because an IBAN with letters converted to numbers can be well over 30 digits long, it exceeds JavaScript's IEEE 754 safe integer ceiling (Number.MAX_SAFE_INTEGER = 9,007,199,254,740,991, approximately 16 digits). Naive division results in rounding errors that falsify valid IBANs.
Modern implementations solve this with BigInt:
// Robust IBAN validator using BigInt
function validateIBAN(iban) {
const clean = iban.replace(/[\s-]/g, '').toUpperCase();
if (!/^[A-Z]{2}\d{2}[A-Z0-9]{4,30}$/.test(clean)) return false;
// Rearrange: move first 4 characters to the end
const rearranged = clean.slice(4) + clean.slice(0, 4);
// Convert letters A-Z to numbers 10-35
const numericString = rearranged.replace(/[A-Z]/g, char => char.charCodeAt(0) - 55);
// Perform modulo 97 division using BigInt
return BigInt(numericString) % 97n === 1n;
}
console.log(validateIBAN("PL61109010140000071219812874")); // Returns true
Production Pro-Tip: The Luhn 09 vs 90 blind spot
While the Luhn algorithm successfully detects 100 percent of single-digit typos (e.g., entering 5 instead of 6), it possesses a known mathematical blind spot regarding transposition errors involving 0 and 9.
Because doubling 0 yields 0 and doubling 9 yields 18 (which sums to 1 + 8 = 9), swapping adjacent digits 09 to 90 (or 90 to 09) produces identical checksum sums:
(0 * 2) + 9 = 9(9 * 2) + 0 = (18 -> 9) + 0 = 9
The Luhn algorithm cannot detect transposition of 09 and 90. ISO 7064 MOD 97-10 has no such blind spot for adjacent digits: swapping digits a and b changes the number by (a - b) x 9 x 10^k, and since 97 is a prime that divides none of these factors, every such swap changes the remainder.
When building financial test suites, generating synthetic test data with valid checksums is essential to avoid using real customer account numbers. For QA testing of Polish and European banking identifiers, use a client-side test data generator to produce valid PESEL, NIP, and IBAN numbers locally. For calculating transactional tax breakdowns, net/gross splits, and legally required currency text for invoices, use an in-browser VAT calculator.
Frequently Asked Questions (FAQ)
Does a valid Luhn checksum guarantee a credit card is active?
No. The Luhn algorithm verifies structural mathematical validity only. It cannot verify whether an account exists, whether funds are available, or whether a card has expired. That verification requires a live authorization call to the card network.
Why does IBAN validation require a remainder of 1 instead of 0?
It is a convention of the MOD 97-10 scheme. The check digits are calculated as 98 minus the remainder (mod 97) of the rearranged number with 00 in place of the check digits. As a result, a correct IBAN always leaves a remainder of 1, and the check digits always fall between 02 and 98.
What is the maximum length of an IBAN?
Under ISO 13616, an IBAN has a maximum fixed length of 34 alphanumeric characters. However, each country establishes a specific fixed length within that ceiling (e.g., France is 27 characters, Germany is 22 characters, the UK is 22 characters, and Poland is 28 characters).
References & Official Sources
- SWIFT: IBAN - International Bank Account Number (ISO 13616 registration authority and IBAN registry)
- US Patent 2,950,048: Computer for verifying numbers (H. P. Luhn)
- ISO/IEC 7812-1:2017 Identification cards - Identification of issuers - Part 1: Numbering system
- ISO 13616-1:2020 Financial services - International bank account number (IBAN)
- ISO/IEC 7064:2003 Information technology - Security techniques - Check character systems