Skip to content

Commit 19f11cf

Browse files
authored
feat: added isAbaRouting validator (#2143)
Added isAbaRouting validator, new validator. Check whether a string is ABA routing number for US bank account & cheque ref: https://en.wikipedia.org/wiki/ABA_routing_transit_number
1 parent 32b174e commit 19f11cf

4 files changed

Lines changed: 43 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Validator | Description
9090
--------------------------------------- | --------------------------------------
9191
**contains(str, seed [, options])** | check if the string contains the seed.<br/><br/>`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.<br />Options: <br/> `ignoreCase`: Ignore case when doing comparison, default false.<br/>`minOccurences`: Minimum number of occurrences for the seed in the string. Defaults to 1.
9292
**equals(str, comparison)** | check if the string matches the comparison.
93+
**isAbaRouting(str)** | check if the string is an ABA routing number for US bank account / cheque.
9394
**isAfter(str [, options])** | check if the string is a date that is after the specified date.<br/><br/>`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.<br/>**Options:**<br/>`comparisonDate`: Date to compare to. Defaults to `Date().toString()` (now).
9495
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']` and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
9596
**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bn', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import isTime from './lib/isTime';
1818
import isBoolean from './lib/isBoolean';
1919
import isLocale from './lib/isLocale';
2020

21+
import isAbaRouting from './lib/isAbaRouting';
2122
import isAlpha, { locales as isAlphaLocales } from './lib/isAlpha';
2223
import isAlphanumeric, { locales as isAlphanumericLocales } from './lib/isAlphanumeric';
2324
import isNumeric from './lib/isNumeric';
@@ -146,6 +147,7 @@ const validator = {
146147
isBoolean,
147148
isIBAN,
148149
isBIC,
150+
isAbaRouting,
149151
isAlpha,
150152
isAlphaLocales,
151153
isAlphanumeric,

src/lib/isAbaRouting.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import assertString from './util/assertString';
2+
3+
// http://www.brainjar.com/js/validation/
4+
// https://www.aba.com/news-research/research-analysis/routing-number-policy-procedures
5+
// series reserved for future use are excluded
6+
const isRoutingReg = /^(?!(1[3-9])|(20)|(3[3-9])|(4[0-9])|(5[0-9])|(60)|(7[3-9])|(8[1-9])|(9[0-2])|(9[3-9]))[0-9]{9}$/;
7+
8+
export default function isAbaRouting(str) {
9+
assertString(str);
10+
11+
if (!isRoutingReg.test(str)) return false;
12+
13+
let checkSumVal = 0;
14+
for (let i = 0; i < str.length; i++) {
15+
if (i % 3 === 0) checkSumVal += str[i] * 3;
16+
else if (i % 3 === 1) checkSumVal += str[i] * 7;
17+
else checkSumVal += str[i] * 1;
18+
}
19+
return (checkSumVal % 10 === 0);
20+
}

test/validators.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5298,6 +5298,26 @@ describe('Validators', () => {
52985298
});
52995299
});
53005300

5301+
it('should validate ABA routing number', () => {
5302+
test({
5303+
validator: 'isAbaRouting',
5304+
valid: [
5305+
'322070381',
5306+
'011103093',
5307+
'263170175',
5308+
'124303065',
5309+
],
5310+
invalid: [
5311+
'426317017',
5312+
'789456124',
5313+
'603558459',
5314+
'qwerty',
5315+
'12430306',
5316+
'382070381',
5317+
],
5318+
});
5319+
});
5320+
53015321
it('should validate IBAN', () => {
53025322
test({
53035323
validator: 'isIBAN',

0 commit comments

Comments
 (0)