diff --git a/README.md b/README.md index d92811cfe..3fdce3a08 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ Validator | Description **isSurrogatePair(str)** | check if the string contains any surrogate pairs chars. **isUppercase(str)** | check if the string is uppercase. **isSlug** | Check if the string is of type slug. `Options` allow a single hyphen between string. e.g. [`cn-cn`, `cn-c-c`] +**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US` **isURL(str [, options])** | check if the string is an URL.

`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, disallow_auth: false }`.

require_protocol - if set as true isURL will return false if protocol is not present in the URL.
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.
protocols - valid protocols can be modified with this option.
require_host - if set as false isURL will not check if host is present in the URL.
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed. **isUUID(str [, version])** | check if the string is a UUID (version 3, 4 or 5). **isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars. diff --git a/src/index.js b/src/index.js index 29d20cec0..7de1ea3a0 100644 --- a/src/index.js +++ b/src/index.js @@ -74,6 +74,7 @@ import isEAN from './lib/isEAN'; import isISIN from './lib/isISIN'; import isISBN from './lib/isISBN'; import isISSN from './lib/isISSN'; +import isTaxID from './lib/isTaxID'; import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone'; @@ -207,6 +208,7 @@ const validator = { normalizeEmail, toString, isSlug, + isTaxID, isDate, }; diff --git a/src/lib/isTaxID.js b/src/lib/isTaxID.js new file mode 100644 index 000000000..6d607a271 --- /dev/null +++ b/src/lib/isTaxID.js @@ -0,0 +1,68 @@ +import assertString from './util/assertString'; + +/** + * An Employer Identification Number (EIN), also known as a Federal Tax Identification Number, + * is used to identify a business entity. + * + * NOTES: + * - Prefix 47 is being reserved for future use + * - Prefixes 26, 27, 45, 46 and 47 were previously assigned by the Philadelphia campus. + * + * See `http://www.irs.gov/Businesses/Small-Businesses-&-Self-Employed/How-EINs-are-Assigned-and-Valid-EIN-Prefixes` + * for more information. + */ + + +/** + * Campus prefixes according to locales + */ + +const campusPrefix = { + 'en-US': { + andover: ['10', '12'], + atlanta: ['60', '67'], + austin: ['50', '53'], + brookhaven: ['01', '02', '03', '04', '05', '06', '11', '13', '14', '16', '21', '22', '23', '25', '34', '51', '52', '54', '55', '56', '57', '58', '59', '65'], + cincinnati: ['30', '32', '35', '36', '37', '38', '61'], + fresno: ['15', '24'], + internet: ['20', '26', '27', '45', '46', '47'], + kansas: ['40', '44'], + memphis: ['94', '95'], + ogden: ['80', '90'], + philadelphia: ['33', '39', '41', '42', '43', '46', '48', '62', '63', '64', '66', '68', '71', '72', '73', '74', '75', '76', '77', '81', '82', '83', '84', '85', '86', '87', '88', '91', '92', '93', '98', '99'], + sba: ['31'], + }, +}; + + +function getPrefixes(locale) { + const prefixes = []; + + for (const location in campusPrefix[locale]) { + if (campusPrefix[locale].hasOwnProperty(location)) { + prefixes.push(...campusPrefix[locale][location]); + } + } + + prefixes.sort(); + + return prefixes; +} + +// tax id regex formats for various loacles + +const taxIdFormat = { + + 'en-US': /^\d{2}[- ]{0,1}\d{7}$/, + +}; + + +export default function isTaxID(str, locale = 'en-US') { + assertString(str); + if (!taxIdFormat[locale].test(str)) { + return false; + } + return getPrefixes(locale).indexOf(str.substr(0, 2)) !== -1; +} + diff --git a/test/validators.js b/test/validators.js index 0caf824fd..afa905070 100755 --- a/test/validators.js +++ b/test/validators.js @@ -8357,6 +8357,23 @@ describe('Validators', () => { }); }); + + it('should validate taxID', () => { + test({ + validator: 'isTaxID', + valid: [ + '01-1234567', + '01 1234567', + '011234567'], + invalid: [ + '0-11234567', + '01#1234567', + '01 1234567', + '01 1234 567'], + }); + }); + + it('should validate slug', () => { test({ validator: 'isSlug',