isTaxID.js 39 KB

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