ABA Routing Number Validation
angular.module('root').directive 'abaValid', ->
{
require: 'ngModel'
link: (scope, elm, attr, ctrl) ->
ctrl.$validators.text = (model, view) ->
aba_is_valid(model)
}
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