-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiban.ts
More file actions
30 lines (24 loc) · 969 Bytes
/
Copy pathiban.ts
File metadata and controls
30 lines (24 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
export function validateIBAN(iban: string): { valid: boolean; error?: string } {
const cleaned = iban.replace(/\s/g, '').toUpperCase();
if (!/^DE\d{20}$/.test(cleaned)) {
return { valid: false, error: 'IBAN must be DE followed by 20 digits' };
}
// ISO 7064 Mod 97-10: move first 4 chars to end, convert letters to numbers
const rearranged = cleaned.slice(4) + cleaned.slice(0, 4);
const numeric = rearranged.replace(/[A-Z]/g, (ch) =>
(ch.charCodeAt(0) - 55).toString()
);
// Mod 97 on large number (process in chunks to avoid BigInt)
let remainder = 0;
for (const digit of numeric) {
remainder = (remainder * 10 + parseInt(digit, 10)) % 97;
}
if (remainder !== 1) {
return { valid: false, error: 'IBAN checksum invalid' };
}
return { valid: true };
}
export function extractBIC(blz: string): string | undefined {
// BIC lookup is not implemented; pass BIC explicitly or let the bank resolve it
return undefined;
}