Verify ABA Number


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 &lt; s.length; i++) { c = parseInt(s.charAt(i), 10); if (c &gt;= 0 &amp;&amp; c 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 &lt; 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 &amp;&amp; n % 10 == 0){
return true;
}else{
return false;
}
}

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>