-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathisIBAN.js
183 lines (164 loc) · 6.21 KB
/
isIBAN.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import assertString from './util/assertString';
/**
* List of country codes with
* corresponding IBAN regular expression
* Reference: https://en.wikipedia.org/wiki/International_Bank_Account_Number
*/
const ibanRegexThroughCountryCode = {
AD: /^(AD[0-9]{2})\d{8}[A-Z0-9]{12}$/,
AE: /^(AE[0-9]{2})\d{3}\d{16}$/,
AL: /^(AL[0-9]{2})\d{8}[A-Z0-9]{16}$/,
AT: /^(AT[0-9]{2})\d{16}$/,
AZ: /^(AZ[0-9]{2})[A-Z0-9]{4}\d{20}$/,
BA: /^(BA[0-9]{2})\d{16}$/,
BE: /^(BE[0-9]{2})\d{12}$/,
BG: /^(BG[0-9]{2})[A-Z]{4}\d{6}[A-Z0-9]{8}$/,
BH: /^(BH[0-9]{2})[A-Z]{4}[A-Z0-9]{14}$/,
BR: /^(BR[0-9]{2})\d{23}[A-Z]{1}[A-Z0-9]{1}$/,
BY: /^(BY[0-9]{2})[A-Z0-9]{4}\d{20}$/,
CH: /^(CH[0-9]{2})\d{5}[A-Z0-9]{12}$/,
CR: /^(CR[0-9]{2})\d{18}$/,
CY: /^(CY[0-9]{2})\d{8}[A-Z0-9]{16}$/,
CZ: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VA: /^(VA[0-9]{2})\d{18}$/,
VG: /^(VG[0-9]{2})[A-Z0-9]{4}\d{16}$/,
XK: /^(XK[0-9]{2})\d{16}$/,
};
/**
* Check if the country codes passed are valid using the
* ibanRegexThroughCountryCode as a reference
*
* @param {array} countryCodeArray
* @return {boolean}
*/
function hasOnlyValidCountryCodes(countryCodeArray) {
const countryCodeArrayFilteredWithObjectIbanCode = countryCodeArray
.filter((countryCode) => !(countryCode in ibanRegexThroughCountryCode));
if (countryCodeArrayFilteredWithObjectIbanCode.length > 0) {
return false;
}
return true;
}
/**
* Check whether string has correct universal IBAN format
* The IBAN consists of up to 34 alphanumeric characters, as follows:
* Country Code using ISO 3166-1 alpha-2, two letters
* check digits, two digits and
* Basic Bank Account Number (BBAN), up to 30 alphanumeric characters.
* NOTE: Permitted IBAN characters are: digits [0-9] and the 26 latin alphabetic [A-Z]
*
* @param {string} str - string under validation
* @param {object} options - object to pass the countries to be either whitelisted or blacklisted
* @return {boolean}
*/
function hasValidIbanFormat(str, options) {
// Strip white spaces and hyphens
const strippedStr = str.replace(/[\s\-]+/gi, '').toUpperCase();
const isoCountryCode = strippedStr.slice(0, 2).toUpperCase();
const isoCountryCodeInIbanRegexCodeObject = isoCountryCode in ibanRegexThroughCountryCode;
if (options.whitelist) {
if (!hasOnlyValidCountryCodes(options.whitelist)) {
return false;
}
const isoCountryCodeInWhiteList = options.whitelist.includes(isoCountryCode);
if (!isoCountryCodeInWhiteList) {
return false;
}
}
if (options.blacklist) {
const isoCountryCodeInBlackList = options.blacklist.includes(isoCountryCode);
if (isoCountryCodeInBlackList) {
return false;
}
}
return (isoCountryCodeInIbanRegexCodeObject)
&& ibanRegexThroughCountryCode[isoCountryCode].test(strippedStr);
}
/**
* Check whether string has valid IBAN Checksum
* by performing basic mod-97 operation and
* the remainder should equal 1
* -- Start by rearranging the IBAN by moving the four initial characters to the end of the string
* -- Replace each letter in the string with two digits, A -> 10, B = 11, Z = 35
* -- Interpret the string as a decimal integer and
* -- compute the remainder on division by 97 (mod 97)
* Reference: https://en.wikipedia.org/wiki/International_Bank_Account_Number
*
* @param {string} str
* @return {boolean}
*/
function hasValidIbanChecksum(str) {
const strippedStr = str.replace(/[^A-Z0-9]+/gi, '').toUpperCase(); // Keep only digits and A-Z latin alphabetic
const rearranged = strippedStr.slice(4) + strippedStr.slice(0, 4);
const alphaCapsReplacedWithDigits = rearranged.replace(/[A-Z]/g, (char) => char.charCodeAt(0) - 55);
const remainder = alphaCapsReplacedWithDigits.match(/\d{1,7}/g)
.reduce((acc, value) => Number(acc + value) % 97, '');
return remainder === 1;
}
export default function isIBAN(str, options = {}) {
assertString(str);
return hasValidIbanFormat(str, options) && hasValidIbanChecksum(str);
}
export const locales = Object.keys(ibanRegexThroughCountryCode);