1

Algorithm that calculates the IBAN key and compares it with the key entered in iban:

  • Remove the country code and key
  • Put the country code and a key 00 at the end
  • convert characters in number (A=10; B=11; ......)
  • calculate modulo 97
  • remove the result at 98
  • you have the key

The modulo function is rewritten for large numbers

check my answer below as a solution

Estelle
  • 51
  • 4

1 Answers1

3
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;
}
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
Estelle
  • 51
  • 4
  • thank you for sharing own solution. did you consider using 3rd party package for that(https://www.npmjs.com/package/iban)? could you also add a link to any kind of official docs which described validation logic? I'm surprised it is not easy to find. Say iso.org [proposes me](https://www.iso.org/standard/41031.html) to buy standard docs. – skyboyer Jul 26 '19 at 17:56
  • and just in case: you may use `parseInt(compete[i], 36)` to convert alphanumeric into dec long number – skyboyer Jul 26 '19 at 17:57
  • 1
    @skyboyer — You can find the validation rules [on wikipedia](https://en.wikipedia.org/wiki/International_Bank_Account_Number) as well. – Ulysse BN Jul 27 '19 at 08:39
  • a good example of how to find modulo - https://stackoverflow.com/a/49573540/9783262 – SAndriy Apr 12 '21 at 08:52