Skip to content

ABARoutingNumber: Add ABA Routing Number Validation #2216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/additional/abaRoutingNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* This is used in the United States to process payments, deposits,
* or transfers using the Automated Clearing House (ACH) or Fedwire
* systems. A very common use case would be to validate a form for
* an ACH bill payment.
*/
$.validator.addMethod( "abaRoutingNumber", function( value ) {
var checksum = 0;
var tokens = value.split( "" );
var length = tokens.length;

// Length Check
if ( length !== 9 ) {
return false;
}

// Calc the checksum
// https://en.wikipedia.org/wiki/ABA_routing_transit_number
for ( var i = 0; i < length; i += 3 ) {
checksum += parseInt( tokens[ i ], 10 ) * 3 +
parseInt( tokens[ i + 1 ], 10 ) * 7 +
parseInt( tokens[ i + 2 ], 10 );
}

// If not zero and divisible by 10 then valid
if ( checksum !== 0 && checksum % 10 === 0 ) {
return true;
}

return false;
}, "Please enter a valid routing number." );
23 changes: 23 additions & 0 deletions test/additional/abaRoutingNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
QUnit.test( "abaRoutingNumber", function( assert ) {
var method = methodTest( "abaRoutingNumber" );

// Bounds
assert.ok( !method( "12345678" ), "Invalid routing number." );
assert.ok( !method( "1234567890" ), "Invalid routing number." );

// Checksum
assert.ok( !method( "123456789" ), "Invalid routing number." );
assert.ok( method( "123456780" ), "Valid routing number." );
assert.ok( method( "123123123" ), "Valid routing number." );
assert.ok( method( "021000021" ), "Valid routing number." );
assert.ok( method( "011401533" ), "Valid routing number." );
assert.ok( method( "091000019" ), "Valid routing number." );

// Garbage
assert.ok( !method( "asdf" ), "Invalid routing number." );
assert.ok( !method( "asdfasdfa" ), "Invalid routing number." );
assert.ok( !method( "&&abcdefg" ), "Invalid routing number." );
assert.ok( !method( "||abcdefg" ), "Invalid routing number." );
assert.ok( !method( "!abcdefgh" ), "Invalid routing number." );
assert.ok( !method( "abcd+efgh" ), "Invalid routing number." );
} );
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<script src="methods.js"></script>
<script src="additional/creditcard.js"></script>
<script src="additional/netmask.js"></script>
<script src="additional/abaRoutingNumber.js"></script>
<script src="aria.js"></script>
<script src="error-placement.js"></script>
</head>
Expand Down