Create The Internet… It's What I Do EVERY Day – Joseph Yancey

30Aug/09N/A0

Bank Routing Number (ABA) Algorithm

At work, I have recently had to work on some validation for bank routing numbers.  Through much research, I have discovered the algorithm.  Check

The number that I am talking about here is the ABA Routing Number.

We'll start with a routing number like 231381116. Here's how the algorithm works. First, strip out any non-numeric characters (like dashes or spaces) and makes sure the resulting string's length is nine digits,

2 3 1 3 8 1 1 1 6

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.

(2 x 3) + (3 x 7) + (1 x 1) +
(3x 3) + (8 x 7) + (1 x 1) +
(1 x 3) + (1 x 7) + (6 x 1) = 110

If the resulting number is an integer multiple of 10, then the number is valid. To calculate what the checksum digit should be, follow the above algorithm for the first 8 digits. In the case above, you would come up with 104. Thus, to make the total number an integer multiple of 10, the final check digit must be 6.

Enough with the details, on with the algorithm!

PHP5

  1. function validateABA($number){
  2.    //strip everything but numbers (dashes and whatnot)
  3.    $number = ereg_replace("[^0-9]", "", $number );
  4.  
  5.    //check the length of the number
  6.    if(strlen($number) != 9){
  7.       return false;
  8.    }
  9.  
  10.    //split the number into an array
  11.    $number = str_split($number);
  12.  
  13.    //run the number through our algorithm
  14.    $rval = $number[0] * 3;
  15.    $rval = ($number[1] * 7) + $rval;
  16.    $rval = ($number[2] * 1) + $rval;
  17.    $rval = ($number[3] * 3) + $rval;
  18.    $rval = ($number[4] * 7) + $rval;
  19.    $rval = ($number[5] * 1) + $rval;
  20.    $rval = ($number[6] * 3) + $rval;
  21.    $rval = ($number[7] * 7) + $rval;
  22.    $rval = ($number[8] * 1) + $rval;
  23.  
  24.    // check if it is an integer multiple of 10
  25.    if(is_int($rval/10){
  26.       return $rval;
  27.    }else{
  28.       return false;
  29.    }
  30. }
blog comments powered by Disqus

Buy Me a Beer

Well not really a beer
More like a Dr Pepper...