我需要使用JavaScript进行IBAN验证检查。我需要遵守的规则是:
验证IBAN通过将IBAN转换为整数并对其执行基本的mod-97操作(如ISO 7064中所述)来验证IBAN。如果IBAN有效,则余数等于1。
-
根据国家/地区检查IBAN总长度是否正确。如果不是,则IBAN是无效的
-
将四个初始字符移动到字符串的末尾
-
将字符串中的每个字母替换为两位数字,从而扩展字符串,其中A=10,B=11。。。,Z=35
-
将字符串解释为十进制整数,并计算该数字除以97 的余数
我这样做是为了白俄罗斯IBAN,所以它必须遵循以下格式
2C 31N-
RU1230000000000000000000000000000
如何修改以下内容以满足上述规则;
function validateIBAN(iban) {
var newIban = iban.toUpperCase(),
modulo = function(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;
};
if (newIban.search(/^[A-Z]{2}/gi) < 0) {
return false;
}
newIban = newIban.substring(4) + newIban.substring(0, 4);
newIban = newIban.replace(/[A-Z]/g, function(match) {
return match.charCodeAt(0) - 55;
});
return parseInt(modulo(newIban, 97), 10) === 1;
}
console.log(validateIBAN("RU1230000000000000000000000000000"));
基于http://toms-cafe.de/iban/iban.js我已经开发了我的IBAN验证检查版本。
您可以通过修改变量CODE_LENGTHS
来修改国家/地区支持。
这是我的实现:
function alertValidIBAN(iban) {
alert(isValidIBANNumber(iban));
}
/*
* Returns 1 if the IBAN is valid
* Returns FALSE if the IBAN's length is not as should be (for CY the IBAN Should be 28 chars long starting with CY )
* Returns any other number (checksum) when the IBAN is invalid (check digits do not match)
*/
function isValidIBANNumber(input) {
var CODE_LENGTHS = {
AD: 24, AE: 23, AT: 20, AZ: 28, BA: 20, BE: 16, BG: 22, BH: 22, BR: 29,
CH: 21, CR: 21, CY: 28, CZ: 24, DE: 22, DK: 18, DO: 28, EE: 20, ES: 24,
FI: 18, FO: 18, FR: 27, GB: 22, GI: 23, GL: 18, GR: 27, GT: 28, HR: 21,
HU: 28, IE: 22, IL: 23, IS: 26, IT: 27, JO: 30, KW: 30, KZ: 20, LB: 28,
LI: 21, LT: 20, LU: 20, LV: 21, MC: 27, MD: 24, ME: 22, MK: 19, MR: 27,
MT: 31, MU: 30, NL: 18, NO: 15, PK: 24, PL: 28, PS: 29, PT: 25, QA: 29,
RO: 24, RS: 22, SA: 24, SE: 24, SI: 19, SK: 24, SM: 27, TN: 24, TR: 26,
AL: 28, BY: 28, CR: 22, EG: 29, GE: 22, IQ: 23, LC: 32, SC: 31, ST: 25,
SV: 28, TL: 23, UA: 29, VA: 22, VG: 24, XK: 20
};
var iban = String(input).toUpperCase().replace(/[^A-Z0-9]/g, ''), // keep only alphanumeric characters
code = iban.match(/^([A-Z]{2})(d{2})([A-Zd]+)$/), // match and capture (1) the country code, (2) the check digits, and (3) the rest
digits;
// check syntax and length
if (!code || iban.length !== CODE_LENGTHS[code[1]]) {
return false;
}
// rearrange country code and check digits, and convert chars to ints
digits = (code[3] + code[1] + code[2]).replace(/[A-Z]/g, function (letter) {
return letter.charCodeAt(0) - 55;
});
// final check
return mod97(digits) === 1;
}
function mod97(string) {
var checksum = string.slice(0, 2), fragment;
for (var offset = 2; offset < string.length; offset += 7) {
fragment = String(checksum) + string.substring(offset, offset + 7);
checksum = parseInt(fragment, 10) % 97;
}
return checksum;
}
input { width:300px; }
Enter IBAN
<input type="test" id="iban"/>
<button onclick="alertValidIBAN(document.getElementById('iban').value);">check IBAN</button>
您可以使用此库来验证和格式化IBAN:https://github.com/arhs/iban.js(免责声明:我写了图书馆)
然而,俄罗斯和白俄罗斯都不受支持,因为我在维基百科的IBAN页面上或官方IBAN注册中心都找不到任何提及这些国家的信息,所以恐怕你必须自己修改库代码才能添加。
我回答了一个类似的问题。使用此正则表达式列表来验证您的IBANS。有70个国家,所以总的来说你应该很乐意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})s?
GB[a-zA-Z0-9]{2}s?([a-zA-Z]{4}s?){1}([0-9]{4}s?){3}([0-9]{2})s?
VA[a-zA-Z0-9]{2}s?([0-9]{3})([0-9]{1}s?)([0-9]{4}s?){3}([0-9]{2})s?
VG[a-zA-Z0-9]{2}s?([a-zA-Z0-9]{4}s?){1}([0-9]{4}s?){4}s?
"Jquery验证插件"也支持iban检查:
https://github.com/jzaefferer/jquery-validation/blob/master/src/additional/iban.js
基于前面的答案,我创建了一个实现AngularValidator
方法的Directive
我还添加了类型。
iban-validator.directive.ts
import { Validator, AbstractControl, NG_VALIDATORS } from '@angular/forms';
import { Directive } from '@angular/core';
@Directive({
selector: '[ibanValidators]',
providers: [{
provide: NG_VALIDATORS,
useExisting: IbanValidatorDirective,
multi: true
}]
})
export class IbanValidatorDirective implements Validator {
validate(control: AbstractControl): object {
const codeLengths = {
AD: 24, AE: 23, AL: 28, AT: 20, AZ: 28, BA: 20, BE: 16, BG: 22, BH: 22, BR: 29, CH: 21, CR: 21, CY: 28, CZ: 24,
DE: 22, DK: 18, DO: 28, EE: 20, ES: 24, LC: 30, FI: 18, FO: 18, FR: 27, GB: 22, GI: 23, GL: 18, GR: 27, GT: 28,
HR: 21, HU: 28, IE: 22, IL: 23, IS: 26, IT: 27, JO: 30, KW: 30, KZ: 20, LB: 28, LI: 21, LT: 20, LU: 20, LV: 21,
MC: 27, MD: 24, ME: 22, MK: 19, MR: 27, MT: 31, MU: 30, NL: 18, NO: 15, PK: 24, PL: 28, PS: 29, PT: 25, QA: 29,
RO: 24, RS: 22, SA: 24, SE: 24, SI: 19, SK: 24, SM: 27, TN: 24, TR: 26
};
if (control.value) {
const iban = control.value.toUpperCase().replace(/[^A-Z0-9]/g, '');
const code = iban.match(/^([A-Z]{2})(d{2})([A-Zd]+)$/);
let digits: number;
if (!code || iban.length !== codeLengths[code[1]]) {
return { ibanValid: false };
}
digits = (code[3] + code[1] + code[2]).replace(/[A-Z]/g, (letter: string) => {
return letter.charCodeAt(0) - 55;
});
return this.mod97(digits) === 1 ? null : { ibanValid: false };
}
}
private mod97(digital: number | string) {
digital = digital.toString();
let checksum: number | string = digital.slice(0, 2);
let fragment = '';
for (let offset = 2; offset < digital.length; offset += 7) {
fragment = checksum + digital.substring(offset, offset + 7);
checksum = parseInt(fragment, 10) % 97;
}
return checksum;
}
}
然后只需对输入使用指令。
我知道这是一个老话题,但由于它出现在谷歌的第一位,而且这里的解决方案并没有真正完善,我想向你展示我对它的看法:
export const validIban = (value) => {
let rearrange =
value.substring(4, value.length)
+ value.substring(0, 4);
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
let alphaMap = {};
let number = [];
alphabet.forEach((value, index) => {
alphaMap[value] = index + 10;
});
rearrange.split('').forEach((value, index) => {
number[index] = alphaMap[value] || value;
});
return modulo(number.join('').toString(), 97) === 1;
}
const modulo = (aNumStr, aDiv) => {
var tmp = "";
var i, r;
for (i = 0; i < aNumStr.length; i++) {
tmp += aNumStr.charAt(i);
r = tmp % aDiv;
tmp = r.toString();
}
return tmp / 1;
}
它对我来说效果很好,你可以用它来测试validIban(testThis)
/http://randomiban.com
此处提到的所有国家/地区的可复制/粘贴Typescript代码:
function validateIban(input: string) {
const codeLengths = {
AD: 24,
AE: 23,
AL: 28,
AT: 20,
AZ: 28,
BA: 20,
BE: 16,
BG: 22,
BH: 22,
BI: 28,
BR: 29,
BY: 28,
CH: 21,
CR: 22,
CY: 28,
CZ: 24,
DE: 22,
DK: 18,
DO: 28,
EE: 20,
EG: 29,
ES: 24,
LC: 32,
FI: 18,
FO: 18,
FR: 27,
GB: 22,
GE: 22,
GI: 23,
GL: 18,
GR: 27,
GT: 28,
HR: 21,
HU: 28,
IE: 22,
IL: 23,
IQ: 23,
IS: 26,
IT: 27,
JO: 30,
KW: 30,
KZ: 20,
LB: 28,
LI: 21,
LT: 20,
LU: 20,
LV: 21,
LY: 25,
MC: 27,
MD: 24,
ME: 22,
MK: 19,
MR: 27,
MT: 31,
MU: 30,
NL: 18,
NO: 15,
PK: 24,
PL: 28,
PS: 29,
PT: 25,
QA: 29,
RO: 24,
RS: 22,
SA: 24,
SC: 31,
SD: 18,
SE: 24,
SI: 19,
SK: 24,
SM: 27,
ST: 25,
SV: 28,
TL: 23,
TN: 24,
TR: 26,
UA: 29,
VA: 22,
VG: 24,
XK: 20,
};
const iban = input.toUpperCase().replace(/[^A-Z0-9]/g, "");
const code = iban.match(/^([A-Z]{2})(d{2})([A-Zd]+)$/);
if (!code || iban.length !== (codeLengths as any)[code[1]]) {
return false;
}
const digits = (code[3] + code[1] + code[2]).replace(/[A-Z]/g, (letter: string) => {
return (letter.charCodeAt(0) - 55).toString();
});
return mod97(digits) === 1;
}
和校验和:
function mod97(digital: number | string) {
digital = digital.toString();
let checksum: number | string = digital.slice(0, 2);
let fragment = "";
for (let offset = 2; offset < digital.length; offset += 7) {
fragment = checksum + digital.substring(offset, offset + 7);
checksum = parseInt(fragment, 10) % 97;
}
return checksum;
}
我写了两个正则表达式,第一个仅适用于SEPA国家,另一个适用于所有支持IBAN的国家,截至2021-10年。
适用于SEPA区域内国家/地区的IBAN正则表达式
第一个检查IBAN在SEPA区域内是否有效,IBAN必须属于SEPA区域的国家。(它认为SEPA区域外的IBAN无效)
^(?:((?:IT|SM)d{2}[A-Z]{1}d{22})|(NLd{2}[A-Z]{4}d{10})|(LVd{2}[A-Z]{4}d{13})|((?:BG|GB|IE)d{2}[A-Z]{4}d{14})|(GId{2}[A-Z]{4}d{15})|(ROd{2}[A-Z]{4}d{16})|(MTd{2}[A-Z]{4}d{23})|(NOd{13})|((?:DK|FI)d{16})|((?:SI)d{17})|((?:AT|EE|LU|LT)d{18})|((?:HR|LI|CH)d{19})|((?:DE|VA)d{20})|((?:AD|CZ|ES|MD|SK|SE)d{22})|(PTd{23})|((?:IS)d{24})|((?:BE)d{14})|((?:FR|MC|GR)d{25})|((?:PL|HU|CY)d{26}))$
规格:
- https://www.iban.com/structure
- https://docs.google.com/spreadsheets/d/1376mu58jQ2P5jGX7tIXGrFfwgfo0RHO7RBRSpKcViIs/edit?usp=sharing基于上述数据结构的Google Sheet,更易于使用
测试:https://regex101.com/r/oLRltj/1/
适用于任何国家/地区的IBAN正则表达式
^(?:(?:CR|DE|ME|RS|VA)d{20}|(?:CZ|ES|SE|SK|TN)d{22}|(?:DK|FI|FO|GL|SD)d{16}|(?:AT|BA|EE|LT|XK)d{18}|(?:AE|IL|TL)d{21}|(?:LY|PT|ST)d{23}|(?:IT|SM)d{2}[A-Z]d{10}[A-Za-z0-9]{12}|(?:HU|PL)d{26}|(?:AL|CY)d{10}[A-Za-z0-9]{16}|(?:CH|LI)d{7}[A-Za-z0-9]{12}|(?:FR|MC)d{12}[A-Za-z0-9]{11}d{2}|(?:GB|IE)d{2}[A-Z]{4}d{14}|(?:KZ|LU)d{5}[A-Za-z0-9]{13}|(?:GI|IQ)d{2}[A-Z]{4}[A-Za-z0-9]{15}|(?:PK|RO)d{2}[A-Z]{4}[A-Za-z0-9]{16}|(?:PS|QA)d{2}[A-Z]{4}[A-Za-z0-9]{21}|ADd{10}[A-Za-z0-9]{12}|AZd{2}[A-Z]{4}[A-Za-z0-9]{20}|BEd{14}|BGd{2}[A-Z]{4}d{6}[A-Za-z0-9]{8}|BHd{2}[A-Z]{4}[A-Za-z0-9]{14}|BRd{25}[A-Z][A-Za-z0-9]|BYd{2}[A-Za-z0-9]{4}d{4}[A-Za-z0-9]{16}|DOd{2}[A-Za-z0-9]{4}d{20}|EGd{27}|GEd{2}[A-Z]d{16}|GTd{2}[A-Za-z0-9]{24}|GRd{9}[A-Za-z0-9]{16}|HRd{19}|ISd{24}|JOd{2}[A-Z]{4}d{4}[A-Za-z0-9]{18}|KWd{2}[A-Z]{4}[A-Za-z0-9]{22}|LCd{2}[A-Z]{4}[A-Za-z0-9]{24}|LBd{6}[A-Za-z0-9]{20}|LVd{2}[A-Z]{4}d{13}|MDd{2}[A-Za-z0-9]{20}|MKd{5}[A-Za-z0-9]{10}d{2}|MRd{25}|MTd{2}[A-Z]{4}d{5}[A-Za-z0-9]{18}|MUd{2}[A-Z]{4}d{19}[A-Z]{3}|NLd{2}[A-Z]{4}d{10}|NOd{13}|SAd{4}[A-Za-z0-9]{18}|SCd{2}[A-Z]{4}d{20}[A-Z]{3}|SId{17}|SVd{2}[A-Z]{4}d{20}|TRd{8}[A-Za-z0-9]{16}|UAd{8}[A-Za-z0-9]{19}|VGd{2}[A-Z]{4}d{16}|GEd{2}[A-Z]{2}d{16})$
第二个正则表达式取自https://stackoverflow.com/a/69068004/2391795,并稍作修改,以增加对通用电气(格鲁吉亚)国家的适当支持。
测试:https://regex101.com/r/I3oiuo/1/
只需使用此网站api:转到https://api.ibanapi.com然后得到apk_key
iban = SA4705000068203056854000 api_key = ea649b36c1d1648bf0f12df82a8da475e2ee3447 url = 'https://api.ibanapi.com/v1/validate/'iban+'?api_key='+api_key+''
IBAN检查器
- 检查IBAN的结构并为验证引擎做好准备
- 确保国家支持Swift公共注册的IBAN
- 使用校验数字验证IBAN
- 如果支持国家级验证,它将贯穿始终
- 从我们的注册处获取此IBAN的银行信息
根据上一份SWIFT IBAN注册机构出版物(2021年6月),我更新了Rony Nasr的REGEX提案。它包括79个国家,并检查特定国家的IBAN结构和长度。(但是,它不进行校验和MOD97验证)。
/^(?:(?:CR|DE|ME|RS|VA)d{20}|(?:CZ|ES|SE|SK|TN)d{22}|(?:DK|FI|FO|GL|SD)d{16}|(?:AT|BA|EE|LT|XK)d{18}|(?:AE|IL|TL)d{21}|(?:LY|PT|ST)d{23}|(?:IT|SM)d{2}[A-Z]d{10}[A-Za-z0-9]{12}|(?:HU|PL)d{26}|(?:AL|CY)d{10}[A-Za-z0-9]{16}|(?:CH|LI)d{7}[A-Za-z0-9]{12}|(?:FR|MC)d{12}[A-Za-z0-9]{11}d{2}|(?:GB|IE)d{2}[A-Z]{4}d{14}|(?:KZ|LU)d{5}[A-Za-z0-9]{13}|(?:GI|IQ)d{2}[A-Z]{4}[A-Za-z0-9]{15}|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