Skip to content

Commit

Permalink
feat: added isAbaRouting validator (#2143)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
songyuew committed Apr 25, 2024
1 parent 32b174e commit 19f11cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -90,6 +90,7 @@ Validator | Description
--------------------------------------- | --------------------------------------
**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.
**equals(str, comparison)** | check if the string matches the comparison.
**isAbaRouting(str)** | check if the string is an ABA routing number for US bank account / cheque.
**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).
**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.
**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.
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -18,6 +18,7 @@ import isTime from './lib/isTime';
import isBoolean from './lib/isBoolean';
import isLocale from './lib/isLocale';

import isAbaRouting from './lib/isAbaRouting';
import isAlpha, { locales as isAlphaLocales } from './lib/isAlpha';
import isAlphanumeric, { locales as isAlphanumericLocales } from './lib/isAlphanumeric';
import isNumeric from './lib/isNumeric';
Expand Down Expand Up @@ -146,6 +147,7 @@ const validator = {
isBoolean,
isIBAN,
isBIC,
isAbaRouting,
isAlpha,
isAlphaLocales,
isAlphanumeric,
Expand Down
20 changes: 20 additions & 0 deletions src/lib/isAbaRouting.js
@@ -0,0 +1,20 @@
import assertString from './util/assertString';

// http://www.brainjar.com/js/validation/
// https://www.aba.com/news-research/research-analysis/routing-number-policy-procedures
// series reserved for future use are excluded
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}$/;

export default function isAbaRouting(str) {
assertString(str);

if (!isRoutingReg.test(str)) return false;

let checkSumVal = 0;
for (let i = 0; i < str.length; i++) {
if (i % 3 === 0) checkSumVal += str[i] * 3;
else if (i % 3 === 1) checkSumVal += str[i] * 7;
else checkSumVal += str[i] * 1;
}
return (checkSumVal % 10 === 0);
}
20 changes: 20 additions & 0 deletions test/validators.test.js
Expand Up @@ -5298,6 +5298,26 @@ describe('Validators', () => {
});
});

it('should validate ABA routing number', () => {
test({
validator: 'isAbaRouting',
valid: [
'322070381',
'011103093',
'263170175',
'124303065',
],
invalid: [
'426317017',
'789456124',
'603558459',
'qwerty',
'12430306',
'382070381',
],
});
});

it('should validate IBAN', () => {
test({
validator: 'isIBAN',
Expand Down

0 comments on commit 19f11cf

Please sign in to comment.