gpt4 book ai didi

javascript - IBAN 验证检查

转载 作者:行者123 更新时间:2023-12-03 02:57:27 26 4
gpt4 key购买 nike

我需要使用 JavaScript 进行 IBAN 验证检查。我需要遵循的规则是:

验证 IBANIBAN 的验证方法是将其转换为整数并对其执行基本 mod-97 运算(如 ISO 7064 中所述)。如果 IBAN 有效,则余数等于 1。

  1. 检查 IBAN 总长度是否符合所在国家/地区的要求。如果不是,则 IBAN 无效

  2. 将四个起始字符移至字符串末尾

  3. 将字符串中的每个字母替换为两位数字,从而扩展字符串,其中 A = 10、B = 11、...、Z = 35

  4. 将字符串解释为十进制整数并计算该数字除以 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-Z\d]+)$/), // 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>

关于javascript - IBAN 验证检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21928083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com