Validating a Bank Routing Number on a Web Form
Have you ever had to collect a routing number and bank account number on a web form?
I’ve worked on a lot of software where we collected this information (I’ve spent a lot of time on banking systems), but never on a web form. One thing I remember is that the routing number has a check digit, so that only one number out of 10 is an actual valid routing number. The routine for calculating that check digit is universal, at least for banks that go through the US Federal Reserve.
Here’s a writeup of how that digit gets calculated, copied from a doc that I wrote in 1999 (I’m so old!).
The program will provide a validation of the ABA route/transit number entered for a bank where ACH deductions are being set up. The following logic is used for this validation:
The 1st, 4th, and 7th digits are multiplied by 3. The 2nd, 5th, and 8th digits are multiplied by 7. The 3rd, 6th, and 9th digits are multiplied by 1.
Sum all of the multiplication results and divide by 10. The remainder must be zero.
Routing Number = 0 5 3 1 0 0 3 0 0
Multipliers 3 7 1 3 7 1 3 7 1
Products 0 35 3 3 0 0 9 0 00 + 35 + 3 + 3 + 0 + 0 + 9 + 0 + 0 = 50.
Since 50 is divisible by 10, this is an acceptable number.
I’ve run into the case where we need to validate it on a web form, so I implemented this validation in a javascript function. It’s pretty self-explanatory.
1 2 3 4 5 6 7 8 9 10 11 |
function isValidRoutingNumber(n) { if (n.length !== 9 || isNaN(n)) { return false; } else digits = n.split(""); var sum = digits[0] * 7 + digits[1] * 3 + digits[2] + digits[3] * 7 + digits[4] * 3 + digits[5] + digits[6] * 7 + digits[7] * 3 + digits[8]; return sum % 10 === 0; } |
So, you just pass the number from your input as the parameter n, and it returns a boolean true if it’s valid, and false if it’s not.