【问题标题】:calculate and check an IBAN [duplicate]计算并检查 IBAN [重复]
【发布时间】:2019-12-05 01:40:00
【问题描述】:

计算 IBAN 密钥并将其与 iban 中输入的密钥进行比较的算法:

  • 删除国家代码和密钥
  • 将国家代码和键 00 放在末尾​​li>
  • 将字符转换为数字(A=10;B=11;......)
  • 计算模 97
  • 删除98处的结果
  • 你有钥匙

为大数重写了模函数

在下面查看我的答案作为解决方案

【问题讨论】:

  • 这里的问题是什么?
  • @TomO。他们只是与社区分享解决方案。它是如此命名为“问答格式”,因此您可以创建。
  • @skyboyer Gotcha,不知道 - 谢谢!

标签: javascript iban


【解决方案1】:
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;
}

【讨论】:

  • 感谢您分享自己的解决方案。您是否考虑过为此使用 3rd 方包(npmjs.com/package/iban)?您是否还可以添加任何描述验证逻辑的官方文档的链接?我很惊讶它不容易找到。说 iso.org proposes me 购买标准文档。
  • 以防万一:您可以使用parseInt(compete[i], 36) 将字母数字转换为十进制长数字
  • @skyboyer — 你也可以找到验证规则on wikipedia
  • 如何求模的一个很好的例子 - stackoverflow.com/a/49573540/9783262
猜你喜欢
  • 1970-01-01
  • 2014-03-22
  • 2013-06-16
  • 2017-11-23
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 2013-12-22
相关资源
最近更新 更多