Tuesday, June 9, 2009

Grails Validator for bank routing numbers

US Bank routing numbers have a checksum constraint built in. A Grails validator for that constraint is simple.


routingNum (blank:false, matches:/^\d{9}$/,
validator: {val, obj ->
if (val?.size() < 9) return false
def n = 0;
for (def i = 0; i < val.size(); i += 3) {
n += val[i].toInteger() * 3
n += val[i + 1].toInteger() * 7
n += val[i + 2].toInteger();
}
return (n != 0 && n % 10 == 0)
})

No comments: