MainelyDesign.com Blog

PHP Function to Validate Bank Routing Numbers

Posted on 03/16/2010 at 04:10 pm by Kevin Wentworth
Viewed 22,363 times | 0 comments

I needed to validate that a Bank Routing Number was valid, much like the algorithms out there to check if a Credit Card number is valid.  I did a little searching and didn't find a PHP function that would do the trick.  I decided to write my own, based on this javascript function, and publish it for anyone who wants to use it:

Function to Validate Bank Routing Numbers:

  1. function checkRoutingNumber($routingNumber = 0) {
  2.     $routingNumber = preg_replace('[\D]', '', $routingNumber); //only digits
  3.     if(strlen($routingNumber) != 9) {
  4.         return false;  
  5.     }
  6.                
  7.     $checkSum = 0;
  8.     for ($i = 0, $j = strlen($routingNumber); $i < $j; $i+= 3 ) {
  9.         //loop through routingNumber character by character
  10.         $checkSum += ($routingNumber[$i] * 3);
  11.         $checkSum += ($routingNumber[$i+1] * 7);
  12.         $checkSum += ($routingNumber[$i+2]);
  13.     }
  14.                
  15.     if($checkSum != 0 and ($checkSum % 10) == 0) {
  16.         return true;
  17.     } else {
  18.         return false;
  19.     }
  20. }

Cheers,
-Kevin Wentworth

Bookmark and Share

Tags for PHP Function to Validate Bank Routing Numbers

Php | Web Programming | Example

Comments for this Posting

No comments. Be the first to post a reply.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design