diff --git a/src/libs/actions/BankAccounts.js b/src/libs/actions/BankAccounts.js index ca64aac7cd22..96d7bd107ca3 100644 --- a/src/libs/actions/BankAccounts.js +++ b/src/libs/actions/BankAccounts.js @@ -820,6 +820,28 @@ function updateReimbursementAccountDraft(bankAccountData) { Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT_DRAFT, bankAccountData); } +/** + * Checks the given number is a valid US Routing Number + * using ABA routingNumber checksum algorithm: http://www.brainjar.com/js/validation/ + * @param {String} number + * @returns {Boolean} + */ +function validateRoutingNumber(number) { + let n = 0; + for (let i = 0; i < number.length; i += 3) { + n += (parseInt(number.charAt(i), 10) * 3) + + (parseInt(number.charAt(i + 1), 10) * 7) + + parseInt(number.charAt(i + 2), 10); + } + + // If the resulting sum is an even multiple of ten (but not zero), + // the ABA routing number is valid. + if (n !== 0 && n % 10 === 0) { + return true; + } + return false; +} + export { activateWallet, addPersonalBankAccount, @@ -839,4 +861,5 @@ export { setWorkspaceIDForReimbursementAccount, setBankAccountSubStep, updateReimbursementAccountDraft, + validateRoutingNumber, }; diff --git a/src/pages/ReimbursementAccount/BankAccountStep.js b/src/pages/ReimbursementAccount/BankAccountStep.js index f76c0d659bcc..ae24c6fa3192 100644 --- a/src/pages/ReimbursementAccount/BankAccountStep.js +++ b/src/pages/ReimbursementAccount/BankAccountStep.js @@ -25,6 +25,7 @@ import { setupWithdrawalAccount, showBankAccountErrorModal, updateReimbursementAccountDraft, + validateRoutingNumber, } from '../../libs/actions/BankAccounts'; import ONYXKEYS from '../../ONYXKEYS'; import compose from '../../libs/compose'; @@ -86,7 +87,7 @@ class BankAccountStep extends React.Component { if (!CONST.BANK_ACCOUNT.REGEX.IBAN.test(this.state.accountNumber.trim())) { errors.accountNumber = true; } - if (!CONST.BANK_ACCOUNT.REGEX.SWIFT_BIC.test(this.state.routingNumber.trim())) { + if (!CONST.BANK_ACCOUNT.REGEX.SWIFT_BIC.test(this.state.routingNumber.trim()) || !validateRoutingNumber(this.state.routingNumber.trim())) { errors.routingNumber = true; } if (!this.state.hasAcceptedTerms) {