isTaxID.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  2. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  3. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  4. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
  5. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  6. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  7. import assertString from './util/assertString';
  8. import * as algorithms from './util/algorithms';
  9. import isDate from './isDate';
  10. /**
  11. * TIN Validation
  12. * Validates Tax Identification Numbers (TINs) from the US, EU member states and the United Kingdom.
  13. *
  14. * EU-UK:
  15. * National TIN validity is calculated using public algorithms as made available by DG TAXUD.
  16. *
  17. * See `https://ec.europa.eu/taxation_customs/tin/specs/FS-TIN%20Algorithms-Public.docx` for more information.
  18. *
  19. * US:
  20. * An Employer Identification Number (EIN), also known as a Federal Tax Identification Number,
  21. * is used to identify a business entity.
  22. *
  23. * NOTES:
  24. * - Prefix 47 is being reserved for future use
  25. * - Prefixes 26, 27, 45, 46 and 47 were previously assigned by the Philadelphia campus.
  26. *
  27. * See `http://www.irs.gov/Businesses/Small-Businesses-&-Self-Employed/How-EINs-are-Assigned-and-Valid-EIN-Prefixes`
  28. * for more information.
  29. */
  30. // Locale functions
  31. /*
  32. * bg-BG validation function
  33. * (Edinen graždanski nomer (EGN/ЕГН), persons only)
  34. * Checks if birth date (first six digits) is valid and calculates check (last) digit
  35. */
  36. function bgBgCheck(tin) {
  37. // Extract full year, normalize month and check birth date validity
  38. var century_year = tin.slice(0, 2);
  39. var month = parseInt(tin.slice(2, 4), 10);
  40. if (month > 40) {
  41. month -= 40;
  42. century_year = "20".concat(century_year);
  43. } else if (month > 20) {
  44. month -= 20;
  45. century_year = "18".concat(century_year);
  46. } else {
  47. century_year = "19".concat(century_year);
  48. }
  49. if (month < 10) {
  50. month = "0".concat(month);
  51. }
  52. var date = "".concat(century_year, "/").concat(month, "/").concat(tin.slice(4, 6));
  53. if (!isDate(date, 'YYYY/MM/DD')) {
  54. return false;
  55. } // split digits into an array for further processing
  56. var digits = tin.split('').map(function (a) {
  57. return parseInt(a, 10);
  58. }); // Calculate checksum by multiplying digits with fixed values
  59. var multip_lookup = [2, 4, 8, 5, 10, 9, 7, 3, 6];
  60. var checksum = 0;
  61. for (var i = 0; i < multip_lookup.length; i++) {
  62. checksum += digits[i] * multip_lookup[i];
  63. }
  64. checksum = checksum % 11 === 10 ? 0 : checksum % 11;
  65. return checksum === digits[9];
  66. }
  67. /**
  68. * Check if an input is a valid Canadian SIN (Social Insurance Number)
  69. *
  70. * The Social Insurance Number (SIN) is a 9 digit number that
  71. * you need to work in Canada or to have access to government programs and benefits.
  72. *
  73. * https://en.wikipedia.org/wiki/Social_Insurance_Number
  74. * https://www.canada.ca/en/employment-social-development/services/sin.html
  75. * https://www.codercrunch.com/challenge/819302488/sin-validator
  76. *
  77. * @param {string} input
  78. * @return {boolean}
  79. */
  80. function isCanadianSIN(input) {
  81. var digitsArray = input.split('');
  82. var even = digitsArray.filter(function (_, idx) {
  83. return idx % 2;
  84. }).map(function (i) {
  85. return Number(i) * 2;
  86. }).join('').split('');
  87. var total = digitsArray.filter(function (_, idx) {
  88. return !(idx % 2);
  89. }).concat(even).map(function (i) {
  90. return Number(i);
  91. }).reduce(function (acc, cur) {
  92. return acc + cur;
  93. });
  94. return total % 10 === 0;
  95. }
  96. /*
  97. * cs-CZ validation function
  98. * (Rodné číslo (RČ), persons only)
  99. * Checks if birth date (first six digits) is valid and divisibility by 11
  100. * Material not in DG TAXUD document sourced from:
  101. * -`https://lorenc.info/3MA381/overeni-spravnosti-rodneho-cisla.htm`
  102. * -`https://www.mvcr.cz/clanek/rady-a-sluzby-dokumenty-rodne-cislo.aspx`
  103. */
  104. function csCzCheck(tin) {
  105. tin = tin.replace(/\W/, ''); // Extract full year from TIN length
  106. var full_year = parseInt(tin.slice(0, 2), 10);
  107. if (tin.length === 10) {
  108. if (full_year < 54) {
  109. full_year = "20".concat(full_year);
  110. } else {
  111. full_year = "19".concat(full_year);
  112. }
  113. } else {
  114. if (tin.slice(6) === '000') {
  115. return false;
  116. } // Three-zero serial not assigned before 1954
  117. if (full_year < 54) {
  118. full_year = "19".concat(full_year);
  119. } else {
  120. return false; // No 18XX years seen in any of the resources
  121. }
  122. } // Add missing zero if needed
  123. if (full_year.length === 3) {
  124. full_year = [full_year.slice(0, 2), '0', full_year.slice(2)].join('');
  125. } // Extract month from TIN and normalize
  126. var month = parseInt(tin.slice(2, 4), 10);
  127. if (month > 50) {
  128. month -= 50;
  129. }
  130. if (month > 20) {
  131. // Month-plus-twenty was only introduced in 2004
  132. if (parseInt(full_year, 10) < 2004) {
  133. return false;
  134. }
  135. month -= 20;
  136. }
  137. if (month < 10) {
  138. month = "0".concat(month);
  139. } // Check date validity
  140. var date = "".concat(full_year, "/").concat(month, "/").concat(tin.slice(4, 6));
  141. if (!isDate(date, 'YYYY/MM/DD')) {
  142. return false;
  143. } // Verify divisibility by 11
  144. if (tin.length === 10) {
  145. if (parseInt(tin, 10) % 11 !== 0) {
  146. // Some numbers up to and including 1985 are still valid if
  147. // check (last) digit equals 0 and modulo of first 9 digits equals 10
  148. var checkdigit = parseInt(tin.slice(0, 9), 10) % 11;
  149. if (parseInt(full_year, 10) < 1986 && checkdigit === 10) {
  150. if (parseInt(tin.slice(9), 10) !== 0) {
  151. return false;
  152. }
  153. } else {
  154. return false;
  155. }
  156. }
  157. }
  158. return true;
  159. }
  160. /*
  161. * de-AT validation function
  162. * (Abgabenkontonummer, persons/entities)
  163. * Verify TIN validity by calling luhnCheck()
  164. */
  165. function deAtCheck(tin) {
  166. return algorithms.luhnCheck(tin);
  167. }
  168. /*
  169. * de-DE validation function
  170. * (Steueridentifikationsnummer (Steuer-IdNr.), persons only)
  171. * Tests for single duplicate/triplicate value, then calculates ISO 7064 check (last) digit
  172. * Partial implementation of spec (same result with both algorithms always)
  173. */
  174. function deDeCheck(tin) {
  175. // Split digits into an array for further processing
  176. var digits = tin.split('').map(function (a) {
  177. return parseInt(a, 10);
  178. }); // Fill array with strings of number positions
  179. var occurences = [];
  180. for (var i = 0; i < digits.length - 1; i++) {
  181. occurences.push('');
  182. for (var j = 0; j < digits.length - 1; j++) {
  183. if (digits[i] === digits[j]) {
  184. occurences[i] += j;
  185. }
  186. }
  187. } // Remove digits with one occurence and test for only one duplicate/triplicate
  188. occurences = occurences.filter(function (a) {
  189. return a.length > 1;
  190. });
  191. if (occurences.length !== 2 && occurences.length !== 3) {
  192. return false;
  193. } // In case of triplicate value only two digits are allowed next to each other
  194. if (occurences[0].length === 3) {
  195. var trip_locations = occurences[0].split('').map(function (a) {
  196. return parseInt(a, 10);
  197. });
  198. var recurrent = 0; // Amount of neighbour occurences
  199. for (var _i = 0; _i < trip_locations.length - 1; _i++) {
  200. if (trip_locations[_i] + 1 === trip_locations[_i + 1]) {
  201. recurrent += 1;
  202. }
  203. }
  204. if (recurrent === 2) {
  205. return false;
  206. }
  207. }
  208. return algorithms.iso7064Check(tin);
  209. }
  210. /*
  211. * dk-DK validation function
  212. * (CPR-nummer (personnummer), persons only)
  213. * Checks if birth date (first six digits) is valid and assigned to century (seventh) digit,
  214. * and calculates check (last) digit
  215. */
  216. function dkDkCheck(tin) {
  217. tin = tin.replace(/\W/, ''); // Extract year, check if valid for given century digit and add century
  218. var year = parseInt(tin.slice(4, 6), 10);
  219. var century_digit = tin.slice(6, 7);
  220. switch (century_digit) {
  221. case '0':
  222. case '1':
  223. case '2':
  224. case '3':
  225. year = "19".concat(year);
  226. break;
  227. case '4':
  228. case '9':
  229. if (year < 37) {
  230. year = "20".concat(year);
  231. } else {
  232. year = "19".concat(year);
  233. }
  234. break;
  235. default:
  236. if (year < 37) {
  237. year = "20".concat(year);
  238. } else if (year > 58) {
  239. year = "18".concat(year);
  240. } else {
  241. return false;
  242. }
  243. break;
  244. } // Add missing zero if needed
  245. if (year.length === 3) {
  246. year = [year.slice(0, 2), '0', year.slice(2)].join('');
  247. } // Check date validity
  248. var date = "".concat(year, "/").concat(tin.slice(2, 4), "/").concat(tin.slice(0, 2));
  249. if (!isDate(date, 'YYYY/MM/DD')) {
  250. return false;
  251. } // Split digits into an array for further processing
  252. var digits = tin.split('').map(function (a) {
  253. return parseInt(a, 10);
  254. });
  255. var checksum = 0;
  256. var weight = 4; // Multiply by weight and add to checksum
  257. for (var i = 0; i < 9; i++) {
  258. checksum += digits[i] * weight;
  259. weight -= 1;
  260. if (weight === 1) {
  261. weight = 7;
  262. }
  263. }
  264. checksum %= 11;
  265. if (checksum === 1) {
  266. return false;
  267. }
  268. return checksum === 0 ? digits[9] === 0 : digits[9] === 11 - checksum;
  269. }
  270. /*
  271. * el-CY validation function
  272. * (Arithmos Forologikou Mitroou (AFM/ΑΦΜ), persons only)
  273. * Verify TIN validity by calculating ASCII value of check (last) character
  274. */
  275. function elCyCheck(tin) {
  276. // split digits into an array for further processing
  277. var digits = tin.slice(0, 8).split('').map(function (a) {
  278. return parseInt(a, 10);
  279. });
  280. var checksum = 0; // add digits in even places
  281. for (var i = 1; i < digits.length; i += 2) {
  282. checksum += digits[i];
  283. } // add digits in odd places
  284. for (var _i2 = 0; _i2 < digits.length; _i2 += 2) {
  285. if (digits[_i2] < 2) {
  286. checksum += 1 - digits[_i2];
  287. } else {
  288. checksum += 2 * (digits[_i2] - 2) + 5;
  289. if (digits[_i2] > 4) {
  290. checksum += 2;
  291. }
  292. }
  293. }
  294. return String.fromCharCode(checksum % 26 + 65) === tin.charAt(8);
  295. }
  296. /*
  297. * el-GR validation function
  298. * (Arithmos Forologikou Mitroou (AFM/ΑΦΜ), persons/entities)
  299. * Verify TIN validity by calculating check (last) digit
  300. * Algorithm not in DG TAXUD document- sourced from:
  301. * - `http://epixeirisi.gr/%CE%9A%CE%A1%CE%99%CE%A3%CE%99%CE%9C%CE%91-%CE%98%CE%95%CE%9C%CE%91%CE%A4%CE%91-%CE%A6%CE%9F%CE%A1%CE%9F%CE%9B%CE%9F%CE%93%CE%99%CE%91%CE%A3-%CE%9A%CE%91%CE%99-%CE%9B%CE%9F%CE%93%CE%99%CE%A3%CE%A4%CE%99%CE%9A%CE%97%CE%A3/23791/%CE%91%CF%81%CE%B9%CE%B8%CE%BC%CF%8C%CF%82-%CE%A6%CE%BF%CF%81%CE%BF%CE%BB%CE%BF%CE%B3%CE%B9%CE%BA%CE%BF%CF%8D-%CE%9C%CE%B7%CF%84%CF%81%CF%8E%CE%BF%CF%85`
  302. */
  303. function elGrCheck(tin) {
  304. // split digits into an array for further processing
  305. var digits = tin.split('').map(function (a) {
  306. return parseInt(a, 10);
  307. });
  308. var checksum = 0;
  309. for (var i = 0; i < 8; i++) {
  310. checksum += digits[i] * Math.pow(2, 8 - i);
  311. }
  312. return checksum % 11 % 10 === digits[8];
  313. }
  314. /*
  315. * en-GB validation function (should go here if needed)
  316. * (National Insurance Number (NINO) or Unique Taxpayer Reference (UTR),
  317. * persons/entities respectively)
  318. */
  319. /*
  320. * en-IE validation function
  321. * (Personal Public Service Number (PPS No), persons only)
  322. * Verify TIN validity by calculating check (second to last) character
  323. */
  324. function enIeCheck(tin) {
  325. var checksum = algorithms.reverseMultiplyAndSum(tin.split('').slice(0, 7).map(function (a) {
  326. return parseInt(a, 10);
  327. }), 8);
  328. if (tin.length === 9 && tin[8] !== 'W') {
  329. checksum += (tin[8].charCodeAt(0) - 64) * 9;
  330. }
  331. checksum %= 23;
  332. if (checksum === 0) {
  333. return tin[7].toUpperCase() === 'W';
  334. }
  335. return tin[7].toUpperCase() === String.fromCharCode(64 + checksum);
  336. } // Valid US IRS campus prefixes
  337. var enUsCampusPrefix = {
  338. andover: ['10', '12'],
  339. atlanta: ['60', '67'],
  340. austin: ['50', '53'],
  341. 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'],
  342. cincinnati: ['30', '32', '35', '36', '37', '38', '61'],
  343. fresno: ['15', '24'],
  344. internet: ['20', '26', '27', '45', '46', '47'],
  345. kansas: ['40', '44'],
  346. memphis: ['94', '95'],
  347. ogden: ['80', '90'],
  348. 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'],
  349. sba: ['31']
  350. }; // Return an array of all US IRS campus prefixes
  351. function enUsGetPrefixes() {
  352. var prefixes = [];
  353. for (var location in enUsCampusPrefix) {
  354. // https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
  355. // istanbul ignore else
  356. if (enUsCampusPrefix.hasOwnProperty(location)) {
  357. prefixes.push.apply(prefixes, _toConsumableArray(enUsCampusPrefix[location]));
  358. }
  359. }
  360. return prefixes;
  361. }
  362. /*
  363. * en-US validation function
  364. * Verify that the TIN starts with a valid IRS campus prefix
  365. */
  366. function enUsCheck(tin) {
  367. return enUsGetPrefixes().indexOf(tin.slice(0, 2)) !== -1;
  368. }
  369. /*
  370. * es-ES validation function
  371. * (Documento Nacional de Identidad (DNI)
  372. * or Número de Identificación de Extranjero (NIE), persons only)
  373. * Verify TIN validity by calculating check (last) character
  374. */
  375. function esEsCheck(tin) {
  376. // Split characters into an array for further processing
  377. var chars = tin.toUpperCase().split(''); // Replace initial letter if needed
  378. if (isNaN(parseInt(chars[0], 10)) && chars.length > 1) {
  379. var lead_replace = 0;
  380. switch (chars[0]) {
  381. case 'Y':
  382. lead_replace = 1;
  383. break;
  384. case 'Z':
  385. lead_replace = 2;
  386. break;
  387. default:
  388. }
  389. chars.splice(0, 1, lead_replace); // Fill with zeros if smaller than proper
  390. } else {
  391. while (chars.length < 9) {
  392. chars.unshift(0);
  393. }
  394. } // Calculate checksum and check according to lookup
  395. var lookup = ['T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X', 'B', 'N', 'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C', 'K', 'E'];
  396. chars = chars.join('');
  397. var checksum = parseInt(chars.slice(0, 8), 10) % 23;
  398. return chars[8] === lookup[checksum];
  399. }
  400. /*
  401. * et-EE validation function
  402. * (Isikukood (IK), persons only)
  403. * Checks if birth date (century digit and six following) is valid and calculates check (last) digit
  404. * Material not in DG TAXUD document sourced from:
  405. * - `https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Estonia-TIN.pdf`
  406. */
  407. function etEeCheck(tin) {
  408. // Extract year and add century
  409. var full_year = tin.slice(1, 3);
  410. var century_digit = tin.slice(0, 1);
  411. switch (century_digit) {
  412. case '1':
  413. case '2':
  414. full_year = "18".concat(full_year);
  415. break;
  416. case '3':
  417. case '4':
  418. full_year = "19".concat(full_year);
  419. break;
  420. default:
  421. full_year = "20".concat(full_year);
  422. break;
  423. } // Check date validity
  424. var date = "".concat(full_year, "/").concat(tin.slice(3, 5), "/").concat(tin.slice(5, 7));
  425. if (!isDate(date, 'YYYY/MM/DD')) {
  426. return false;
  427. } // Split digits into an array for further processing
  428. var digits = tin.split('').map(function (a) {
  429. return parseInt(a, 10);
  430. });
  431. var checksum = 0;
  432. var weight = 1; // Multiply by weight and add to checksum
  433. for (var i = 0; i < 10; i++) {
  434. checksum += digits[i] * weight;
  435. weight += 1;
  436. if (weight === 10) {
  437. weight = 1;
  438. }
  439. } // Do again if modulo 11 of checksum is 10
  440. if (checksum % 11 === 10) {
  441. checksum = 0;
  442. weight = 3;
  443. for (var _i3 = 0; _i3 < 10; _i3++) {
  444. checksum += digits[_i3] * weight;
  445. weight += 1;
  446. if (weight === 10) {
  447. weight = 1;
  448. }
  449. }
  450. if (checksum % 11 === 10) {
  451. return digits[10] === 0;
  452. }
  453. }
  454. return checksum % 11 === digits[10];
  455. }
  456. /*
  457. * fi-FI validation function
  458. * (Henkilötunnus (HETU), persons only)
  459. * Checks if birth date (first six digits plus century symbol) is valid
  460. * and calculates check (last) digit
  461. */
  462. function fiFiCheck(tin) {
  463. // Extract year and add century
  464. var full_year = tin.slice(4, 6);
  465. var century_symbol = tin.slice(6, 7);
  466. switch (century_symbol) {
  467. case '+':
  468. full_year = "18".concat(full_year);
  469. break;
  470. case '-':
  471. full_year = "19".concat(full_year);
  472. break;
  473. default:
  474. full_year = "20".concat(full_year);
  475. break;
  476. } // Check date validity
  477. var date = "".concat(full_year, "/").concat(tin.slice(2, 4), "/").concat(tin.slice(0, 2));
  478. if (!isDate(date, 'YYYY/MM/DD')) {
  479. return false;
  480. } // Calculate check character
  481. var checksum = parseInt(tin.slice(0, 6) + tin.slice(7, 10), 10) % 31;
  482. if (checksum < 10) {
  483. return checksum === parseInt(tin.slice(10), 10);
  484. }
  485. checksum -= 10;
  486. var letters_lookup = ['A', 'B', 'C', 'D', 'E', 'F', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'];
  487. return letters_lookup[checksum] === tin.slice(10);
  488. }
  489. /*
  490. * fr/nl-BE validation function
  491. * (Numéro national (N.N.), persons only)
  492. * Checks if birth date (first six digits) is valid and calculates check (last two) digits
  493. */
  494. function frBeCheck(tin) {
  495. // Zero month/day value is acceptable
  496. if (tin.slice(2, 4) !== '00' || tin.slice(4, 6) !== '00') {
  497. // Extract date from first six digits of TIN
  498. var date = "".concat(tin.slice(0, 2), "/").concat(tin.slice(2, 4), "/").concat(tin.slice(4, 6));
  499. if (!isDate(date, 'YY/MM/DD')) {
  500. return false;
  501. }
  502. }
  503. var checksum = 97 - parseInt(tin.slice(0, 9), 10) % 97;
  504. var checkdigits = parseInt(tin.slice(9, 11), 10);
  505. if (checksum !== checkdigits) {
  506. checksum = 97 - parseInt("2".concat(tin.slice(0, 9)), 10) % 97;
  507. if (checksum !== checkdigits) {
  508. return false;
  509. }
  510. }
  511. return true;
  512. }
  513. /*
  514. * fr-FR validation function
  515. * (Numéro fiscal de référence (numéro SPI), persons only)
  516. * Verify TIN validity by calculating check (last three) digits
  517. */
  518. function frFrCheck(tin) {
  519. tin = tin.replace(/\s/g, '');
  520. var checksum = parseInt(tin.slice(0, 10), 10) % 511;
  521. var checkdigits = parseInt(tin.slice(10, 13), 10);
  522. return checksum === checkdigits;
  523. }
  524. /*
  525. * fr/lb-LU validation function
  526. * (numéro d’identification personnelle, persons only)
  527. * Verify birth date validity and run Luhn and Verhoeff checks
  528. */
  529. function frLuCheck(tin) {
  530. // Extract date and check validity
  531. var date = "".concat(tin.slice(0, 4), "/").concat(tin.slice(4, 6), "/").concat(tin.slice(6, 8));
  532. if (!isDate(date, 'YYYY/MM/DD')) {
  533. return false;
  534. } // Run Luhn check
  535. if (!algorithms.luhnCheck(tin.slice(0, 12))) {
  536. return false;
  537. } // Remove Luhn check digit and run Verhoeff check
  538. return algorithms.verhoeffCheck("".concat(tin.slice(0, 11)).concat(tin[12]));
  539. }
  540. /*
  541. * hr-HR validation function
  542. * (Osobni identifikacijski broj (OIB), persons/entities)
  543. * Verify TIN validity by calling iso7064Check(digits)
  544. */
  545. function hrHrCheck(tin) {
  546. return algorithms.iso7064Check(tin);
  547. }
  548. /*
  549. * hu-HU validation function
  550. * (Adóazonosító jel, persons only)
  551. * Verify TIN validity by calculating check (last) digit
  552. */
  553. function huHuCheck(tin) {
  554. // split digits into an array for further processing
  555. var digits = tin.split('').map(function (a) {
  556. return parseInt(a, 10);
  557. });
  558. var checksum = 8;
  559. for (var i = 1; i < 9; i++) {
  560. checksum += digits[i] * (i + 1);
  561. }
  562. return checksum % 11 === digits[9];
  563. }
  564. /*
  565. * lt-LT validation function (should go here if needed)
  566. * (Asmens kodas, persons/entities respectively)
  567. * Current validation check is alias of etEeCheck- same format applies
  568. */
  569. /*
  570. * it-IT first/last name validity check
  571. * Accepts it-IT TIN-encoded names as a three-element character array and checks their validity
  572. * Due to lack of clarity between resources ("Are only Italian consonants used?
  573. * What happens if a person has X in their name?" etc.) only two test conditions
  574. * have been implemented:
  575. * Vowels may only be followed by other vowels or an X character
  576. * and X characters after vowels may only be followed by other X characters.
  577. */
  578. function itItNameCheck(name) {
  579. // true at the first occurence of a vowel
  580. var vowelflag = false; // true at the first occurence of an X AFTER vowel
  581. // (to properly handle last names with X as consonant)
  582. var xflag = false;
  583. for (var i = 0; i < 3; i++) {
  584. if (!vowelflag && /[AEIOU]/.test(name[i])) {
  585. vowelflag = true;
  586. } else if (!xflag && vowelflag && name[i] === 'X') {
  587. xflag = true;
  588. } else if (i > 0) {
  589. if (vowelflag && !xflag) {
  590. if (!/[AEIOU]/.test(name[i])) {
  591. return false;
  592. }
  593. }
  594. if (xflag) {
  595. if (!/X/.test(name[i])) {
  596. return false;
  597. }
  598. }
  599. }
  600. }
  601. return true;
  602. }
  603. /*
  604. * it-IT validation function
  605. * (Codice fiscale (TIN-IT), persons only)
  606. * Verify name, birth date and codice catastale validity
  607. * and calculate check character.
  608. * Material not in DG-TAXUD document sourced from:
  609. * `https://en.wikipedia.org/wiki/Italian_fiscal_code`
  610. */
  611. function itItCheck(tin) {
  612. // Capitalize and split characters into an array for further processing
  613. var chars = tin.toUpperCase().split(''); // Check first and last name validity calling itItNameCheck()
  614. if (!itItNameCheck(chars.slice(0, 3))) {
  615. return false;
  616. }
  617. if (!itItNameCheck(chars.slice(3, 6))) {
  618. return false;
  619. } // Convert letters in number spaces back to numbers if any
  620. var number_locations = [6, 7, 9, 10, 12, 13, 14];
  621. var number_replace = {
  622. L: '0',
  623. M: '1',
  624. N: '2',
  625. P: '3',
  626. Q: '4',
  627. R: '5',
  628. S: '6',
  629. T: '7',
  630. U: '8',
  631. V: '9'
  632. };
  633. for (var _i4 = 0, _number_locations = number_locations; _i4 < _number_locations.length; _i4++) {
  634. var i = _number_locations[_i4];
  635. if (chars[i] in number_replace) {
  636. chars.splice(i, 1, number_replace[chars[i]]);
  637. }
  638. } // Extract month and day, and check date validity
  639. var month_replace = {
  640. A: '01',
  641. B: '02',
  642. C: '03',
  643. D: '04',
  644. E: '05',
  645. H: '06',
  646. L: '07',
  647. M: '08',
  648. P: '09',
  649. R: '10',
  650. S: '11',
  651. T: '12'
  652. };
  653. var month = month_replace[chars[8]];
  654. var day = parseInt(chars[9] + chars[10], 10);
  655. if (day > 40) {
  656. day -= 40;
  657. }
  658. if (day < 10) {
  659. day = "0".concat(day);
  660. }
  661. var date = "".concat(chars[6]).concat(chars[7], "/").concat(month, "/").concat(day);
  662. if (!isDate(date, 'YY/MM/DD')) {
  663. return false;
  664. } // Calculate check character by adding up even and odd characters as numbers
  665. var checksum = 0;
  666. for (var _i5 = 1; _i5 < chars.length - 1; _i5 += 2) {
  667. var char_to_int = parseInt(chars[_i5], 10);
  668. if (isNaN(char_to_int)) {
  669. char_to_int = chars[_i5].charCodeAt(0) - 65;
  670. }
  671. checksum += char_to_int;
  672. }
  673. var odd_convert = {
  674. // Maps of characters at odd places
  675. A: 1,
  676. B: 0,
  677. C: 5,
  678. D: 7,
  679. E: 9,
  680. F: 13,
  681. G: 15,
  682. H: 17,
  683. I: 19,
  684. J: 21,
  685. K: 2,
  686. L: 4,
  687. M: 18,
  688. N: 20,
  689. O: 11,
  690. P: 3,
  691. Q: 6,
  692. R: 8,
  693. S: 12,
  694. T: 14,
  695. U: 16,
  696. V: 10,
  697. W: 22,
  698. X: 25,
  699. Y: 24,
  700. Z: 23,
  701. 0: 1,
  702. 1: 0
  703. };
  704. for (var _i6 = 0; _i6 < chars.length - 1; _i6 += 2) {
  705. var _char_to_int = 0;
  706. if (chars[_i6] in odd_convert) {
  707. _char_to_int = odd_convert[chars[_i6]];
  708. } else {
  709. var multiplier = parseInt(chars[_i6], 10);
  710. _char_to_int = 2 * multiplier + 1;
  711. if (multiplier > 4) {
  712. _char_to_int += 2;
  713. }
  714. }
  715. checksum += _char_to_int;
  716. }
  717. if (String.fromCharCode(65 + checksum % 26) !== chars[15]) {
  718. return false;
  719. }
  720. return true;
  721. }
  722. /*
  723. * lv-LV validation function
  724. * (Personas kods (PK), persons only)
  725. * Check validity of birth date and calculate check (last) digit
  726. * Support only for old format numbers (not starting with '32', issued before 2017/07/01)
  727. * Material not in DG TAXUD document sourced from:
  728. * `https://boot.ritakafija.lv/forums/index.php?/topic/88314-personas-koda-algoritms-%C4%8Deksumma/`
  729. */
  730. function lvLvCheck(tin) {
  731. tin = tin.replace(/\W/, ''); // Extract date from TIN
  732. var day = tin.slice(0, 2);
  733. if (day !== '32') {
  734. // No date/checksum check if new format
  735. var month = tin.slice(2, 4);
  736. if (month !== '00') {
  737. // No date check if unknown month
  738. var full_year = tin.slice(4, 6);
  739. switch (tin[6]) {
  740. case '0':
  741. full_year = "18".concat(full_year);
  742. break;
  743. case '1':
  744. full_year = "19".concat(full_year);
  745. break;
  746. default:
  747. full_year = "20".concat(full_year);
  748. break;
  749. } // Check date validity
  750. var date = "".concat(full_year, "/").concat(tin.slice(2, 4), "/").concat(day);
  751. if (!isDate(date, 'YYYY/MM/DD')) {
  752. return false;
  753. }
  754. } // Calculate check digit
  755. var checksum = 1101;
  756. var multip_lookup = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  757. for (var i = 0; i < tin.length - 1; i++) {
  758. checksum -= parseInt(tin[i], 10) * multip_lookup[i];
  759. }
  760. return parseInt(tin[10], 10) === checksum % 11;
  761. }
  762. return true;
  763. }
  764. /*
  765. * mt-MT validation function
  766. * (Identity Card Number or Unique Taxpayer Reference, persons/entities)
  767. * Verify Identity Card Number structure (no other tests found)
  768. */
  769. function mtMtCheck(tin) {
  770. if (tin.length !== 9) {
  771. // No tests for UTR
  772. var chars = tin.toUpperCase().split(''); // Fill with zeros if smaller than proper
  773. while (chars.length < 8) {
  774. chars.unshift(0);
  775. } // Validate format according to last character
  776. switch (tin[7]) {
  777. case 'A':
  778. case 'P':
  779. if (parseInt(chars[6], 10) === 0) {
  780. return false;
  781. }
  782. break;
  783. default:
  784. {
  785. var first_part = parseInt(chars.join('').slice(0, 5), 10);
  786. if (first_part > 32000) {
  787. return false;
  788. }
  789. var second_part = parseInt(chars.join('').slice(5, 7), 10);
  790. if (first_part === second_part) {
  791. return false;
  792. }
  793. }
  794. }
  795. }
  796. return true;
  797. }
  798. /*
  799. * nl-NL validation function
  800. * (Burgerservicenummer (BSN) or Rechtspersonen Samenwerkingsverbanden Informatie Nummer (RSIN),
  801. * persons/entities respectively)
  802. * Verify TIN validity by calculating check (last) digit (variant of MOD 11)
  803. */
  804. function nlNlCheck(tin) {
  805. return algorithms.reverseMultiplyAndSum(tin.split('').slice(0, 8).map(function (a) {
  806. return parseInt(a, 10);
  807. }), 9) % 11 === parseInt(tin[8], 10);
  808. }
  809. /*
  810. * pl-PL validation function
  811. * (Powszechny Elektroniczny System Ewidencji Ludności (PESEL)
  812. * or Numer identyfikacji podatkowej (NIP), persons/entities)
  813. * Verify TIN validity by validating birth date (PESEL) and calculating check (last) digit
  814. */
  815. function plPlCheck(tin) {
  816. // NIP
  817. if (tin.length === 10) {
  818. // Calculate last digit by multiplying with lookup
  819. var lookup = [6, 5, 7, 2, 3, 4, 5, 6, 7];
  820. var _checksum = 0;
  821. for (var i = 0; i < lookup.length; i++) {
  822. _checksum += parseInt(tin[i], 10) * lookup[i];
  823. }
  824. _checksum %= 11;
  825. if (_checksum === 10) {
  826. return false;
  827. }
  828. return _checksum === parseInt(tin[9], 10);
  829. } // PESEL
  830. // Extract full year using month
  831. var full_year = tin.slice(0, 2);
  832. var month = parseInt(tin.slice(2, 4), 10);
  833. if (month > 80) {
  834. full_year = "18".concat(full_year);
  835. month -= 80;
  836. } else if (month > 60) {
  837. full_year = "22".concat(full_year);
  838. month -= 60;
  839. } else if (month > 40) {
  840. full_year = "21".concat(full_year);
  841. month -= 40;
  842. } else if (month > 20) {
  843. full_year = "20".concat(full_year);
  844. month -= 20;
  845. } else {
  846. full_year = "19".concat(full_year);
  847. } // Add leading zero to month if needed
  848. if (month < 10) {
  849. month = "0".concat(month);
  850. } // Check date validity
  851. var date = "".concat(full_year, "/").concat(month, "/").concat(tin.slice(4, 6));
  852. if (!isDate(date, 'YYYY/MM/DD')) {
  853. return false;
  854. } // Calculate last digit by mulitplying with odd one-digit numbers except 5
  855. var checksum = 0;
  856. var multiplier = 1;
  857. for (var _i7 = 0; _i7 < tin.length - 1; _i7++) {
  858. checksum += parseInt(tin[_i7], 10) * multiplier % 10;
  859. multiplier += 2;
  860. if (multiplier > 10) {
  861. multiplier = 1;
  862. } else if (multiplier === 5) {
  863. multiplier += 2;
  864. }
  865. }
  866. checksum = 10 - checksum % 10;
  867. return checksum === parseInt(tin[10], 10);
  868. }
  869. /*
  870. * pt-BR validation function
  871. * (Cadastro de Pessoas Físicas (CPF, persons)
  872. * Cadastro Nacional de Pessoas Jurídicas (CNPJ, entities)
  873. * Both inputs will be validated
  874. */
  875. function ptBrCheck(tin) {
  876. if (tin.length === 11) {
  877. var _sum;
  878. var remainder;
  879. _sum = 0;
  880. if ( // Reject known invalid CPFs
  881. tin === '11111111111' || tin === '22222222222' || tin === '33333333333' || tin === '44444444444' || tin === '55555555555' || tin === '66666666666' || tin === '77777777777' || tin === '88888888888' || tin === '99999999999' || tin === '00000000000') return false;
  882. for (var i = 1; i <= 9; i++) {
  883. _sum += parseInt(tin.substring(i - 1, i), 10) * (11 - i);
  884. }
  885. remainder = _sum * 10 % 11;
  886. if (remainder === 10) remainder = 0;
  887. if (remainder !== parseInt(tin.substring(9, 10), 10)) return false;
  888. _sum = 0;
  889. for (var _i8 = 1; _i8 <= 10; _i8++) {
  890. _sum += parseInt(tin.substring(_i8 - 1, _i8), 10) * (12 - _i8);
  891. }
  892. remainder = _sum * 10 % 11;
  893. if (remainder === 10) remainder = 0;
  894. if (remainder !== parseInt(tin.substring(10, 11), 10)) return false;
  895. return true;
  896. }
  897. if ( // Reject know invalid CNPJs
  898. tin === '00000000000000' || tin === '11111111111111' || tin === '22222222222222' || tin === '33333333333333' || tin === '44444444444444' || tin === '55555555555555' || tin === '66666666666666' || tin === '77777777777777' || tin === '88888888888888' || tin === '99999999999999') {
  899. return false;
  900. }
  901. var length = tin.length - 2;
  902. var identifiers = tin.substring(0, length);
  903. var verificators = tin.substring(length);
  904. var sum = 0;
  905. var pos = length - 7;
  906. for (var _i9 = length; _i9 >= 1; _i9--) {
  907. sum += identifiers.charAt(length - _i9) * pos;
  908. pos -= 1;
  909. if (pos < 2) {
  910. pos = 9;
  911. }
  912. }
  913. var result = sum % 11 < 2 ? 0 : 11 - sum % 11;
  914. if (result !== parseInt(verificators.charAt(0), 10)) {
  915. return false;
  916. }
  917. length += 1;
  918. identifiers = tin.substring(0, length);
  919. sum = 0;
  920. pos = length - 7;
  921. for (var _i10 = length; _i10 >= 1; _i10--) {
  922. sum += identifiers.charAt(length - _i10) * pos;
  923. pos -= 1;
  924. if (pos < 2) {
  925. pos = 9;
  926. }
  927. }
  928. result = sum % 11 < 2 ? 0 : 11 - sum % 11;
  929. if (result !== parseInt(verificators.charAt(1), 10)) {
  930. return false;
  931. }
  932. return true;
  933. }
  934. /*
  935. * pt-PT validation function
  936. * (Número de identificação fiscal (NIF), persons/entities)
  937. * Verify TIN validity by calculating check (last) digit (variant of MOD 11)
  938. */
  939. function ptPtCheck(tin) {
  940. var checksum = 11 - algorithms.reverseMultiplyAndSum(tin.split('').slice(0, 8).map(function (a) {
  941. return parseInt(a, 10);
  942. }), 9) % 11;
  943. if (checksum > 9) {
  944. return parseInt(tin[8], 10) === 0;
  945. }
  946. return checksum === parseInt(tin[8], 10);
  947. }
  948. /*
  949. * ro-RO validation function
  950. * (Cod Numeric Personal (CNP) or Cod de înregistrare fiscală (CIF),
  951. * persons only)
  952. * Verify CNP validity by calculating check (last) digit (test not found for CIF)
  953. * Material not in DG TAXUD document sourced from:
  954. * `https://en.wikipedia.org/wiki/National_identification_number#Romania`
  955. */
  956. function roRoCheck(tin) {
  957. if (tin.slice(0, 4) !== '9000') {
  958. // No test found for this format
  959. // Extract full year using century digit if possible
  960. var full_year = tin.slice(1, 3);
  961. switch (tin[0]) {
  962. case '1':
  963. case '2':
  964. full_year = "19".concat(full_year);
  965. break;
  966. case '3':
  967. case '4':
  968. full_year = "18".concat(full_year);
  969. break;
  970. case '5':
  971. case '6':
  972. full_year = "20".concat(full_year);
  973. break;
  974. default:
  975. } // Check date validity
  976. var date = "".concat(full_year, "/").concat(tin.slice(3, 5), "/").concat(tin.slice(5, 7));
  977. if (date.length === 8) {
  978. if (!isDate(date, 'YY/MM/DD')) {
  979. return false;
  980. }
  981. } else if (!isDate(date, 'YYYY/MM/DD')) {
  982. return false;
  983. } // Calculate check digit
  984. var digits = tin.split('').map(function (a) {
  985. return parseInt(a, 10);
  986. });
  987. var multipliers = [2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9];
  988. var checksum = 0;
  989. for (var i = 0; i < multipliers.length; i++) {
  990. checksum += digits[i] * multipliers[i];
  991. }
  992. if (checksum % 11 === 10) {
  993. return digits[12] === 1;
  994. }
  995. return digits[12] === checksum % 11;
  996. }
  997. return true;
  998. }
  999. /*
  1000. * sk-SK validation function
  1001. * (Rodné číslo (RČ) or bezvýznamové identifikačné číslo (BIČ), persons only)
  1002. * Checks validity of pre-1954 birth numbers (rodné číslo) only
  1003. * Due to the introduction of the pseudo-random BIČ it is not possible to test
  1004. * post-1954 birth numbers without knowing whether they are BIČ or RČ beforehand
  1005. */
  1006. function skSkCheck(tin) {
  1007. if (tin.length === 9) {
  1008. tin = tin.replace(/\W/, '');
  1009. if (tin.slice(6) === '000') {
  1010. return false;
  1011. } // Three-zero serial not assigned before 1954
  1012. // Extract full year from TIN length
  1013. var full_year = parseInt(tin.slice(0, 2), 10);
  1014. if (full_year > 53) {
  1015. return false;
  1016. }
  1017. if (full_year < 10) {
  1018. full_year = "190".concat(full_year);
  1019. } else {
  1020. full_year = "19".concat(full_year);
  1021. } // Extract month from TIN and normalize
  1022. var month = parseInt(tin.slice(2, 4), 10);
  1023. if (month > 50) {
  1024. month -= 50;
  1025. }
  1026. if (month < 10) {
  1027. month = "0".concat(month);
  1028. } // Check date validity
  1029. var date = "".concat(full_year, "/").concat(month, "/").concat(tin.slice(4, 6));
  1030. if (!isDate(date, 'YYYY/MM/DD')) {
  1031. return false;
  1032. }
  1033. }
  1034. return true;
  1035. }
  1036. /*
  1037. * sl-SI validation function
  1038. * (Davčna številka, persons/entities)
  1039. * Verify TIN validity by calculating check (last) digit (variant of MOD 11)
  1040. */
  1041. function slSiCheck(tin) {
  1042. var checksum = 11 - algorithms.reverseMultiplyAndSum(tin.split('').slice(0, 7).map(function (a) {
  1043. return parseInt(a, 10);
  1044. }), 8) % 11;
  1045. if (checksum === 10) {
  1046. return parseInt(tin[7], 10) === 0;
  1047. }
  1048. return checksum === parseInt(tin[7], 10);
  1049. }
  1050. /*
  1051. * sv-SE validation function
  1052. * (Personnummer or samordningsnummer, persons only)
  1053. * Checks validity of birth date and calls luhnCheck() to validate check (last) digit
  1054. */
  1055. function svSeCheck(tin) {
  1056. // Make copy of TIN and normalize to two-digit year form
  1057. var tin_copy = tin.slice(0);
  1058. if (tin.length > 11) {
  1059. tin_copy = tin_copy.slice(2);
  1060. } // Extract date of birth
  1061. var full_year = '';
  1062. var month = tin_copy.slice(2, 4);
  1063. var day = parseInt(tin_copy.slice(4, 6), 10);
  1064. if (tin.length > 11) {
  1065. full_year = tin.slice(0, 4);
  1066. } else {
  1067. full_year = tin.slice(0, 2);
  1068. if (tin.length === 11 && day < 60) {
  1069. // Extract full year from centenarian symbol
  1070. // Should work just fine until year 10000 or so
  1071. var current_year = new Date().getFullYear().toString();
  1072. var current_century = parseInt(current_year.slice(0, 2), 10);
  1073. current_year = parseInt(current_year, 10);
  1074. if (tin[6] === '-') {
  1075. if (parseInt("".concat(current_century).concat(full_year), 10) > current_year) {
  1076. full_year = "".concat(current_century - 1).concat(full_year);
  1077. } else {
  1078. full_year = "".concat(current_century).concat(full_year);
  1079. }
  1080. } else {
  1081. full_year = "".concat(current_century - 1).concat(full_year);
  1082. if (current_year - parseInt(full_year, 10) < 100) {
  1083. return false;
  1084. }
  1085. }
  1086. }
  1087. } // Normalize day and check date validity
  1088. if (day > 60) {
  1089. day -= 60;
  1090. }
  1091. if (day < 10) {
  1092. day = "0".concat(day);
  1093. }
  1094. var date = "".concat(full_year, "/").concat(month, "/").concat(day);
  1095. if (date.length === 8) {
  1096. if (!isDate(date, 'YY/MM/DD')) {
  1097. return false;
  1098. }
  1099. } else if (!isDate(date, 'YYYY/MM/DD')) {
  1100. return false;
  1101. }
  1102. return algorithms.luhnCheck(tin.replace(/\W/, ''));
  1103. } // Locale lookup objects
  1104. /*
  1105. * Tax id regex formats for various locales
  1106. *
  1107. * Where not explicitly specified in DG-TAXUD document both
  1108. * uppercase and lowercase letters are acceptable.
  1109. */
  1110. var taxIdFormat = {
  1111. 'bg-BG': /^\d{10}$/,
  1112. 'cs-CZ': /^\d{6}\/{0,1}\d{3,4}$/,
  1113. 'de-AT': /^\d{9}$/,
  1114. 'de-DE': /^[1-9]\d{10}$/,
  1115. 'dk-DK': /^\d{6}-{0,1}\d{4}$/,
  1116. 'el-CY': /^[09]\d{7}[A-Z]$/,
  1117. 'el-GR': /^([0-4]|[7-9])\d{8}$/,
  1118. 'en-CA': /^\d{9}$/,
  1119. 'en-GB': /^\d{10}$|^(?!GB|NK|TN|ZZ)(?![DFIQUV])[A-Z](?![DFIQUVO])[A-Z]\d{6}[ABCD ]$/i,
  1120. 'en-IE': /^\d{7}[A-W][A-IW]{0,1}$/i,
  1121. 'en-US': /^\d{2}[- ]{0,1}\d{7}$/,
  1122. 'es-ES': /^(\d{0,8}|[XYZKLM]\d{7})[A-HJ-NP-TV-Z]$/i,
  1123. 'et-EE': /^[1-6]\d{6}(00[1-9]|0[1-9][0-9]|[1-6][0-9]{2}|70[0-9]|710)\d$/,
  1124. 'fi-FI': /^\d{6}[-+A]\d{3}[0-9A-FHJ-NPR-Y]$/i,
  1125. 'fr-BE': /^\d{11}$/,
  1126. 'fr-FR': /^[0-3]\d{12}$|^[0-3]\d\s\d{2}(\s\d{3}){3}$/,
  1127. // Conforms both to official spec and provided example
  1128. 'fr-LU': /^\d{13}$/,
  1129. 'hr-HR': /^\d{11}$/,
  1130. 'hu-HU': /^8\d{9}$/,
  1131. 'it-IT': /^[A-Z]{6}[L-NP-V0-9]{2}[A-EHLMPRST][L-NP-V0-9]{2}[A-ILMZ][L-NP-V0-9]{3}[A-Z]$/i,
  1132. 'lv-LV': /^\d{6}-{0,1}\d{5}$/,
  1133. // Conforms both to DG TAXUD spec and original research
  1134. 'mt-MT': /^\d{3,7}[APMGLHBZ]$|^([1-8])\1\d{7}$/i,
  1135. 'nl-NL': /^\d{9}$/,
  1136. 'pl-PL': /^\d{10,11}$/,
  1137. 'pt-BR': /(?:^\d{11}$)|(?:^\d{14}$)/,
  1138. 'pt-PT': /^\d{9}$/,
  1139. 'ro-RO': /^\d{13}$/,
  1140. 'sk-SK': /^\d{6}\/{0,1}\d{3,4}$/,
  1141. 'sl-SI': /^[1-9]\d{7}$/,
  1142. 'sv-SE': /^(\d{6}[-+]{0,1}\d{4}|(18|19|20)\d{6}[-+]{0,1}\d{4})$/
  1143. }; // taxIdFormat locale aliases
  1144. taxIdFormat['lb-LU'] = taxIdFormat['fr-LU'];
  1145. taxIdFormat['lt-LT'] = taxIdFormat['et-EE'];
  1146. taxIdFormat['nl-BE'] = taxIdFormat['fr-BE'];
  1147. taxIdFormat['fr-CA'] = taxIdFormat['en-CA']; // Algorithmic tax id check functions for various locales
  1148. var taxIdCheck = {
  1149. 'bg-BG': bgBgCheck,
  1150. 'cs-CZ': csCzCheck,
  1151. 'de-AT': deAtCheck,
  1152. 'de-DE': deDeCheck,
  1153. 'dk-DK': dkDkCheck,
  1154. 'el-CY': elCyCheck,
  1155. 'el-GR': elGrCheck,
  1156. 'en-CA': isCanadianSIN,
  1157. 'en-IE': enIeCheck,
  1158. 'en-US': enUsCheck,
  1159. 'es-ES': esEsCheck,
  1160. 'et-EE': etEeCheck,
  1161. 'fi-FI': fiFiCheck,
  1162. 'fr-BE': frBeCheck,
  1163. 'fr-FR': frFrCheck,
  1164. 'fr-LU': frLuCheck,
  1165. 'hr-HR': hrHrCheck,
  1166. 'hu-HU': huHuCheck,
  1167. 'it-IT': itItCheck,
  1168. 'lv-LV': lvLvCheck,
  1169. 'mt-MT': mtMtCheck,
  1170. 'nl-NL': nlNlCheck,
  1171. 'pl-PL': plPlCheck,
  1172. 'pt-BR': ptBrCheck,
  1173. 'pt-PT': ptPtCheck,
  1174. 'ro-RO': roRoCheck,
  1175. 'sk-SK': skSkCheck,
  1176. 'sl-SI': slSiCheck,
  1177. 'sv-SE': svSeCheck
  1178. }; // taxIdCheck locale aliases
  1179. taxIdCheck['lb-LU'] = taxIdCheck['fr-LU'];
  1180. taxIdCheck['lt-LT'] = taxIdCheck['et-EE'];
  1181. taxIdCheck['nl-BE'] = taxIdCheck['fr-BE'];
  1182. taxIdCheck['fr-CA'] = taxIdCheck['en-CA']; // Regexes for locales where characters should be omitted before checking format
  1183. var allsymbols = /[-\\\/!@#$%\^&\*\(\)\+\=\[\]]+/g;
  1184. var sanitizeRegexes = {
  1185. 'de-AT': allsymbols,
  1186. 'de-DE': /[\/\\]/g,
  1187. 'fr-BE': allsymbols
  1188. }; // sanitizeRegexes locale aliases
  1189. sanitizeRegexes['nl-BE'] = sanitizeRegexes['fr-BE'];
  1190. /*
  1191. * Validator function
  1192. * Return true if the passed string is a valid tax identification number
  1193. * for the specified locale.
  1194. * Throw an error exception if the locale is not supported.
  1195. */
  1196. export default function isTaxID(str) {
  1197. var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
  1198. assertString(str); // Copy TIN to avoid replacement if sanitized
  1199. var strcopy = str.slice(0);
  1200. if (locale in taxIdFormat) {
  1201. if (locale in sanitizeRegexes) {
  1202. strcopy = strcopy.replace(sanitizeRegexes[locale], '');
  1203. }
  1204. if (!taxIdFormat[locale].test(strcopy)) {
  1205. return false;
  1206. }
  1207. if (locale in taxIdCheck) {
  1208. return taxIdCheck[locale](strcopy);
  1209. } // Fallthrough; not all locales have algorithmic checks
  1210. return true;
  1211. }
  1212. throw new Error("Invalid locale '".concat(locale, "'"));
  1213. }