Skip to content

Instantly share code, notes, and snippets.

@crueber
Last active November 8, 2017 18:57
Show Gist options
  • Save crueber/76dedb95c8f56b0c33f8 to your computer and use it in GitHub Desktop.
Save crueber/76dedb95c8f56b0c33f8 to your computer and use it in GitHub Desktop.
ABA Routing Number Validation
aba_check_digit = (t) ->
7 * (t[0] + t[3] + t[6]) +
3 * (t[1] + t[4] + t[7]) +
9 * (t[2] + t[5])
aba_check_sum = (t) ->
3 * (t[0] + t[3] + t[6]) +
7 * (t[1] + t[4] + t[7]) +
1 * (t[2] + t[5] + t[8])
# Validates that the incoming aba variable is:
# * 9 digits
# * That the number isn't 0
# * The first digit isn't 5 (internal bank routing number)
# * That the checksum digit of numbers 1 through 8 is the 9th
# * That the checksum passes modulo 10
aba_is_valid = (aba) ->
aba = _.map String(aba).split(''), (n) -> parseInt(n)
return false if aba.length isnt 9 or aba[0] is 5
return false if aba_check_digit(aba) % 10 isnt aba[8]
return false if (checksum = aba_check_sum(aba)) is 0 or checksum % 10 isnt 0
true
angular.module('root').directive 'abaValid', ->
{
require: 'ngModel'
link: (scope, elm, attr, ctrl) ->
ctrl.$validators.text = (model, view) ->
aba_is_valid(model)
}
@crueber
Copy link
Author

crueber commented Apr 14, 2015

I included the angular directive, since that's how I'm currently using it, and I figured someone might find it useful. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment