Skip to content

Commit

Permalink
feat: add checkdigit validation for aba routing numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
phlickey committed Jul 27, 2023
1 parent f17afcc commit da0b937
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/routing-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,36 @@ export default function (value: string | unknown): BankValidity {
}

const isValid = value in allowedRoutingNumbers;
const isPotentiallyValid = /^\d{0,8}$/.test(value);
const isPotentiallyValid = isCheckDigitValid(value);

return {
isValid,
isPotentiallyValid: isValid || isPotentiallyValid,
};
}

/**
* reference: https://en.wikipedia.org/wiki/ABA_routing_transit_number#Check_digit
* @param abaRoutingNumber an ABA routing number to be checked
* @returns whether check digit of the ABA routing number is valid
*/
const isCheckDigitValid = (abaRoutingNumber: string) => {
try {
if (!/^\d+$/.test(abaRoutingNumber)) return false;

const d = abaRoutingNumber.split("").map((digit) => parseInt(digit));

const res =
// prettier-ignore
(
(3 * (d[0] + d[3] + d[6])) +
(7 * (d[1] + d[4] + d[7])) +
(1 * (d[2] + d[5] + d[8]))
)
% 10 === 0;

return res;
} catch (e) {
return false;
}
};

0 comments on commit da0b937

Please sign in to comment.