Validate ACH Routing Numbers for EFT and Electronic Checks

I was looking for a way to validate the routing numbers for an eCommerce system I am working on and found a nice VB sample here.

I converted the logic to C# and optimized it for better performance and use.

Hopefully some others out there will find use:

   ///
    /// Validates the routing number.
    ///
    private bool ValidateRoutingNumber(int routingNumber)
    {
        //The check equation for a number a1a2a3a4a5a6a7a8a9 is 3a1 + 7a2 + a3 + 3a4 + 7a5 + a6 + 3a7 + 7 a8 + a9 mod 10 = 0

        //rewrote for higher efficiency

        //must be 9 digits
        if(routingNumber < 100000000)
           return false;
       if(routingNumber > 999999999)
            return false;
        //build array for quick comparison
        //left to right using fun with math.

        int[] routingNumberAsArray =
        {
           routingNumber / 100000000,
            (routingNumber % 100000000) / 10000000,
            (routingNumber % 10000000) / 1000000,
            (routingNumber % 1000000) / 100000,
            (routingNumber % 100000) / 10000,
            (routingNumber % 10000) / 1000,
            (routingNumber % 1000) / 100,
            (routingNumber % 100) / 10,
            (routingNumber % 10) / 1
        };

        //calculate the route sum using the formula above. 3 7 1.
        int routeSum =
            3 * routingNumberAsArray[0] +
            7 * routingNumberAsArray[1] +
            routingNumberAsArray[2] +
            3 * routingNumberAsArray[3] +
            7 *+routingNumberAsArray[4] +
            routingNumberAsArray[5] +
            3* routingNumberAsArray [6] +
            7 * routingNumberAsArray [7] +
            routingNumberAsArray [8] ;
        return routeSum % 10 ==0;
    }

0 Replies to “Validate ACH Routing Numbers for EFT and Electronic Checks”

Leave a Reply

Your email address will not be published. Required fields are marked *