Last active
August 29, 2015 14:15
-
-
Save drywall/4bf6da1e07bdfcbdd911 to your computer and use it in GitHub Desktop.
Checks ABA routing number checksum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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