-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation-module.js
More file actions
30 lines (28 loc) · 920 Bytes
/
validation-module.js
File metadata and controls
30 lines (28 loc) · 920 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
const MAX = 999999999;
const MODULUS = 97;
export function validate_iban(value) {
console.log(`Validating ${value}...`);
if (value === undefined || value.length < 5) {
return false;
}
let modulusResult = calculateModulus(value);
return modulusResult === 1;
}
function calculateModulus(code) {
let reformattedCode = code.substring(4) + code.substring(0, 4);
reformattedCode = reformattedCode.replace(/[A-Z]/g, function (match) {
return match.charCodeAt(0) - 55;
});
let total = 0;
for (let i = 0; i < reformattedCode.length; i++) {
let charValue = reformattedCode.charCodeAt(i) - 48;
if (charValue < 0 || charValue > 35) {
return 0;
}
total = (Number(charValue) > 9 ? total * 100 : total * 10) + charValue;
if (total < MAX) {
total = (total % MODULUS);
}
}
return total % MODULUS;
}