-
Notifications
You must be signed in to change notification settings - Fork 1
/
lei-validator.js
59 lines (47 loc) · 1.3 KB
/
lei-validator.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// credits to: http://stackoverflow.com/users/277174/gilead
function modulo(divident, divisor) {
var cDivident = '';
var cRest = '';
for (var i in divident) {
var cChar = divident[i];
var cOperator = cRest + '' + cDivident + '' + cChar;
if (cOperator < parseInt(divisor)) {
cDivident += '' + cChar;
} else {
cRest = cOperator % divisor;
if (cRest == 0) {
cRest = '';
}
cDivident = '';
}
}
cRest += '' + cDivident;
if (cRest == '') {
cRest = 0;
}
return cRest;
}
// checks if input LEI is valid, returns 1 if yes
function validateLEI(leiCode) {
var code = leiCode.toString();
if (code.length != 20)
return false;
code = code.toUpperCase();
if (code.match(/(00|01|99)$/)) {
return false;
}
var computedCode = '';
for (var i = 0; i < 20; i++) {
var acode = code.charCodeAt(i);
if ((acode >= 65) && (acode <= 90)) {
acode -= 55;
computedCode += acode;
} else if ((acode >= 48) && (acode <= 57)) {
acode -= 48;
computedCode += acode;
} else {
return false;
}
}
return modulo(computedCode, 97) == 1;
}