javascript - 计算并检查 IBAN
问题描述
计算 IBAN 密钥并将其与在 iban 中输入的密钥进行比较的算法:
- 删除国家代码和密钥
- 把国家代码和一个键 00 放在最后
- 将字符转换为数字(A=10;B=11;......)
- 计算模数 97
- 删除 98 处的结果
- 你有钥匙
为大数重写模函数
在下面检查我的答案作为解决方案
解决方案
function IsIbanValid(iban) {
// example "FR76 1020 7000 2104 0210 1346 925"
// "CH10 0023 00A1 0235 0260 1"
var keyIBAN = iban.substring(2, 4); // 76
var compte = iban.substring(4, iban.length );
var compteNum = '';
compte = compte + iban.substring(0, 2);
// convert characters in numbers
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (i = 0; i < compte.length; i++) {
if (isNaN(compte[i]))
for (j = 0; j < alphabet.length; j++) {
if (compte[i] == alphabet[j])
compteNum += (j + 10).toString();
}
else
compteNum += compte[i];
}
compteNum += '00'; // concat 00 for key
// end convert
var result = modulo(compteNum, 97);
if ((98-result) == keyIBAN)
return true;
else
return false;
}
/// modulo for big numbers, (modulo % can't be used)
function modulo(divident, divisor) {
var partLength = 10;
while (divident.length > partLength) {
var part = divident.substring(0, partLength);
divident = (part % divisor) + divident.substring(partLength);
}
return divident % divisor;
}
推荐阅读
- opencv - 使用 OpenCV 检测图像是否有故障
- azure-cosmosdb - 如果分区键值为空,Azure Cosmos 会发生什么
- excel - 在 datepicker Excel 中更改日期后运行 VBA 宏
- azure - Azure EventHub EnqueuedTime 在分区内是否单调递增?
- python - 尝试通过连接变量名称并仅打印一列来从 3D 数组打印
- pandas - 结合箱线图绘制水平线参考
- haskell - 对具有相同行为的两种对象进行建模
- c++ - 什么可能导致 C++ 级别的页面错误
- javascript - 我想更改传单中一层的图标颜色,我有两个点,所有图层都显示为蓝色默认图标
- java - 在 Bitbucket 管道中集成 Java Lombok