Verify ABA Number

From JacobAllred

Jump to: navigation, search

Often times it is useful to see if an ABA (bank routing) number is valid. This function will check for you:

PHP

Takes the ABA number as input, and returns true if valid or false if invalid.

The old code I had up here sucked and relied on other functions, so this is the new and improved version.

function validateABACode($aba_code){
	// Coded by Jacob Allred (20 Mar 2008)
	// Converted to PHP from JS found at:
	// http://www.brainjar.com/js/validation/
	// See that site if you need more details on how ABA codes are validated.
 
	// Make sure it doesn't contain invalid characters
	if($aba_code != preg_replace('/[^0-9]/','',$aba_code)){
		return false;
	}
 
	// Check the length, it should be nine digits.
	if(strlen($aba_code) != 9){
		return false;
	}
 
	// Now run through each digit and calculate the total.
	$n = 0;
	for($i = 0; $i < 9; $i += 3) {
		$n += $aba_code[$i] * 3
		+ $aba_code[$i + 1] * 7
		+ $aba_code[$i + 2];
	}
 
	// 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;
	}
}

JavaScript

function checkABA(s) {
  var i, n, t;
 
  t = "";
  for (i = 0; i < s.length; i++) {
    c = parseInt(s.charAt(i), 10);
    if (c >= 0 && c <= 9)
      t = t + c;
  }
 
  // Check the length, it should be nine digits.
  if (t.length != 9){
    return false;
  }
 
  // Now 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;
  }
}
Personal tools