Skip to content

Instantly share code, notes, and snippets.

@drywall
Last active August 29, 2015 14:15
Show Gist options
  • Save drywall/4bf6da1e07bdfcbdd911 to your computer and use it in GitHub Desktop.
Save drywall/4bf6da1e07bdfcbdd911 to your computer and use it in GitHub Desktop.
Checks ABA routing number checksum
/**
* function to test if a given value (string/int) passes a basic ABA routing number checksum
*/
function isValidABA(aba) {
var numericRegex = /^\d{9}$/,
total = null;
// just in cases
aba = aba.toString();
// make sure it's numeric and of length 9
if ( !numericRegex.test( aba ) ) {
return false;
}
// compute checksum
for (var i=0; i<9; i += 3) {
total += parseInt(aba.charAt(i), 10) * 3
+ parseInt(aba.charAt(i + 1), 10) * 7
+ parseInt(aba.charAt(i + 2), 10);
}
if (total !== 0 && total % 10 === 0){
return true;
}
// still here? That's not good.
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment