Bookmark and Share

Validation Algorithms

This article describes the algorithms for validating bank routing numbers and credit card numbers using the checksum built into the number. While they differ in how they are generated, the technique used for both is similar.

ABA Routing Numbers

ABA (American Banker's Association) routing numbers are used to identify financial institutions when making transactions. This number is usually required when setting up a bank account for direct deposits, automated payments, etc. Or when making payments by "paperless" check over the phone or online.

These routing numbers include a checksum digit which can be used to help verify if a given number is valid or not.

Try out some numbers below. You can usually find your own bank's routing number printed on your checks (look for a nine-digit number at the bottom center of the check). Many institutions also readily provide it on their web site, if you want to find others.

ABA Routing Number:

Note that even if a number passes this test, it does not necessarily mean that it is valid. The number may not, in fact, be assigned to any financial institution.

Unlike credit card numbers, ABA routing numbers are always nine digits long. The first four specify the routing symbol, the next four identify the institution and the last is the checksum digit.

Also, the formula used to generate this checksum digit is different, as described below, although the same principle is used.

The Checksum Algorithm

Here's how the algorithm works. First the code strips out any non-numeric characters (like dashes or spaces) and makes sure the resulting string's length is nine digits,

7 8 9 4 5 6 1 2 4

Then we multiply the first digit by 3, the second by 7, the third by 1, the fourth by 3, the fifth by 7, the sixth by 1, etc., and add them all up.

(7 x 3) + (8 x 7) + (9 x 1) +
(4 x 3) + (5 x 7) + (6 x 1) +
(1 x 3) + (2 x 7) + (4 x 1) = 160

Use your browser's View Source option to see the full source code.

If this sum is an integer multiple of 10 (e.g., 10, 20, 30, 40, 50,...) then the number is valid, as far as the checksum is concerned.

Here's the JavaScript code that sums the digits, where t represents the ABA routing number as a string of digits.

  // Run through each digit and calculate the total.

  n = 0;
  for (i = 0; i < t.length; i += 3) {
    n += parseInt(t.charAt(i),     10) * 3
      +  parseInt(t.charAt(i + 1), 10) * 7
      +  parseInt(t.charAt(i + 2), 10);
  }

  // If the resulting sum is an even multiple of ten (but not zero),
  // the aba routing number is good.

  if (n != 0 && n % 10 == 0)
    return true;
  else
    return false;
}

Once the sum is found, the code checks to see if it's an even multiple of ten (i.e., n % 10 leaves no remainder).

Programming Note

You may have noticed that the checksum digit (4) is included in the sum. Rather than adding up the other digits and calculating the valid checksum value for comparison, it's included in the total to save some programming steps.

In other words, if you had an eight-digit ABA number and wanted to find the correct checksum digit, you'd add those numbers as before.

(7 x 3) + (8 x 7) + (9 x 1) +
(4 x 3) + (5 x 7) + (6 x 1) +
(1 x 3) + (2 x 7) = 156

Using the total (156) you'd take the next highest number that is evenly divisible by ten and subtract the total from this to produce the proper checksum digit (160 - 156 = 4 in this case).

If the total was already an even multiple of ten, say 70, the checksum would simply be zero.

To save a couple of steps in the code, the checksum digit is included in the calculation so that the sum should be an integer multiple of ten.