IBAN Validation check
04:13 21 Feb 2014

I need to do an IBAN validation check using JavaScript. The rules I need to follow are:

Validating the IBAN An IBAN is validated by converting it into an integer and performing a basic mod-97 operation (as described in ISO 7064) on it. If the IBAN is valid, the remainder equals 1.

  1. Check that the total IBAN length is correct as per the country. If not, the IBAN is invalid

  2. Move the four initial characters to the end of the string

  3. Replace each letter in the string with two digits, thereby expanding the string, where A = 10, B = 11, ..., Z = 35

  4. Interpret the string as a decimal integer and compute the remainder of that number on division by 97

I am doing this for a Belarusian IBAN so it has to follow the following format

2C 31N -

RU1230000000000000000000000000000

How do I modify the following to meet the above rules;

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"));

javascript iban