-
Notifications
You must be signed in to change notification settings - Fork 4
/
noValidation.js
87 lines (77 loc) · 2.94 KB
/
noValidation.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module.exports = (function(){
"use strict";
var mod11OfNumberWithControlDigit = function (input) {
var controlNumber = 2,
sumForMod = 0,
i, result;
for (i = input.length - 2; i >= 0; --i) {
sumForMod += input.charAt(i) * controlNumber;
if (++controlNumber > 7)
controlNumber = 2;
}
result = (11 - sumForMod % 11);
return result === 11 ? 0 : result;
};
var accountNumber = function (accountNumber) {
if (!accountNumber) {
return false;
}
accountNumber = accountNumber.toString().replace(/\./g, '');
if (accountNumber.length !== 11) {
return false;
}
return parseInt(accountNumber.charAt(accountNumber.length - 1), 10)
=== mod11OfNumberWithControlDigit(accountNumber);
};
var organizationNumber = function(orgNumber){
orgNumber += '';
if (!orgNumber || orgNumber.length !== 9) {
return false;
}
return parseInt(orgNumber.charAt(orgNumber.length - 1), 10)
=== mod11OfNumberWithControlDigit(orgNumber);
};
var birthNumber = function(birthNumber){
birthNumber = birthNumber.toString();
if(!birthNumber || birthNumber.length !== 11){
return false;
}
var sum = function(number, factors){
var sum = 0;
for(var i = 0, l = factors.length; i < l; ++i){
sum += parseInt(number.charAt(i),10) * factors[i];
}
return sum;
};
var checksum1 = 11 - (_sum(birthNumber, [3, 7, 6, 1, 8, 9, 4, 5, 2]) % 11);
if (checksum1 === 11) checksum1 = 0;
var checksum2 = 11 - (_sum(birthNumber, [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]) % 11);
if (checksum2 === 11) checksum2 = 0;
return checksum1 === parseInt(birthNumber.charAt(9), 10)
&& checksum2 === parseInt(birthNumber.charAt(10), 10);
};
var kidNumber = function(kidNumber){
kidNumber = kidNumber.toString();
var controlDigit = kidNumber.charAt(kidNumber.length - 1);
return parseInt(controlDigit, 10) === mod11OfNumberWithControlDigit(kidNumber)
|| parseInt(controlDigit, 10) === luhnValue(kidNumber);
};
var luhnValue = function(number){
var sum = 0, dbl = 0, i;
for(i = number.length-2; i >= 0; i-=2){
dbl = (parseInt(number.charAt(i), 10) * 2).toString();
sum += parseInt(dbl.charAt(0), 10) + parseInt(dbl.charAt(1) || 0, 10);
}
for(i = number.length -3; i >= 0; i-=2){
sum += parseInt(number.charAt(i), 10);
}
sum = sum.toString();
return 10 - parseInt(sum.charAt(sum.length -1), 10);
};
return {
accountNumber: accountNumber,
organizationNumber: organizationNumber,
birthNumber: birthNumber,
kidNumber: kidNumber
};
})();