RT

Code Snippet: Validate Routing Numbers in Python

The following code snippet can be used for validating the checksum on a ABA routing number.

def valid_routing_number(routing_number: str) -> bool:
    if len(routing_number) != 9:
        return False

    if not routing_number.isnumeric():
        return False

    checksum = (3 * (int(routing_number[0]) + int(routing_number[3]) + int(routing_number[6]))) + \
               (7 * (int(routing_number[1]) + int(routing_number[4]) + int(routing_number[7]))) + \
               (1 * (int(routing_number[2]) + int(routing_number[5]) + int(routing_number[8])))

    return (checksum % 10) == 0