-1

I'm trying to check if a provided IBAN number is correct. While initially asking how to handle large numbers with the modulo operator, I stumbled across the fact, that I need to use the BigInt datatype.

console.log(97009700101075 % 97) // gives the correct result 1
console.log(970097009700101075 % 97) // doesn't give the expected result of 1 
// due to the number being too large for regular int

While this is an easy fix to one specific aspect of validating a provided IBAN, I am still wondering, if there is a better way of handling IBAN validation.

Here you can find a basic algorithm for validating an IBAN.

  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

What is a good way to

  1. split the IBAN string and move the first part to the end
  2. convert the first two letters to number, where A=10 and so on
KorbenDose
  • 797
  • 1
  • 7
  • 23
  • 5
    Consider using BigInt type: `console.log(970097009700101075n % 97n) // outputs 1n` – Dmitry VS Aug 26 '23 at 10:29
  • Your problem occurs one step earlier: `970097009700101075` is already not that number. Just type it in the console... This has nothing to do with the `%` operator. – trincot Aug 26 '23 at 10:52
  • @trincot Thanks for the hint. I already mentioned in the question that I believe the problem already comes from the number being too large. – KorbenDose Aug 26 '23 at 11:00
  • Yes, but there is a difference between a too large number and a number that is different from what you think it is. You wrote *"the modulo operator sometimes gives wrong results"*: it doesn't. It gives correct results. – trincot Aug 26 '23 at 11:02
  • Excuse me for not being precise with my words. When saying I believe the number is too large, I meant that I believe the number is too large for javascript to handle. Everything is clear now. There is a better data type `BigInt` and there are better ways of achieving IBAN validation. This is what SO is for, isn't it? – KorbenDose Aug 26 '23 at 11:06
  • Another potential duplicate target: https://stackoverflow.com/q/21928083/10871073. I think there are quite a few others about IBAN verification. – Adrian Mole Aug 28 '23 at 03:46

1 Answers1

0

You're correct that JavaScript's built-in modulo operator (%) might not work as expected for very large numbers due to precision limitations. However, for tasks like calculating the IBAN check digit, there are libraries that handle these cases more effectively.

For IBAN validation, you should follow the official algorithm which typically involves converting the IBAN to an integer representation and then calculating the modulo. Libraries like iban can handle this correctly:

npm install iban

Here's how you can use the iban library to calculate the IBAN check digit:

const IBAN = require('iban');

const ibanNumber1 = '97009700101075';
const ibanNumber2 = '970097009700101075';

const isIbanValid1 = IBAN.isValid(ibanNumber1);
const isIbanValid2 = IBAN.isValid(ibanNumber2);

console.log(isIbanValid1); // true
console.log(isIbanValid2); // false (invalid IBAN)

if (isIbanValid1) {
  const remainder1 = IBAN.createBBAN(ibanNumber1).toBigInt() % 97n;
  console.log(remainder1); // 1
}

if (isIbanValid2) {
  const remainder2 = IBAN.createBBAN(ibanNumber2).toBigInt() % 97n;
  console.log(remainder2); // 1
}

Using the iban library ensures that the calculations are performed correctly, regardless of the number's size. Make sure to follow the specific validation and calculation methods provided by the official IBAN standard and libraries to avoid errors in your IBAN check digit verification.

Syed Arif Iqbal
  • 1,830
  • 12
  • 16
  • 1
    The problem has nothing to do with the `%` operator. – trincot Aug 26 '23 at 10:54
  • @trincot Thank you, you have mentioned that below the question already. Obviously the title of the question doesn't seem to be optimal. This answer still is helpful. – KorbenDose Aug 26 '23 at 11:01