isIdentityCard.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import assertString from './util/assertString';
  2. import isInt from './isInt';
  3. var validators = {
  4. PL: function PL(str) {
  5. assertString(str);
  6. var weightOfDigits = {
  7. 1: 1,
  8. 2: 3,
  9. 3: 7,
  10. 4: 9,
  11. 5: 1,
  12. 6: 3,
  13. 7: 7,
  14. 8: 9,
  15. 9: 1,
  16. 10: 3,
  17. 11: 0
  18. };
  19. if (str != null && str.length === 11 && isInt(str, {
  20. allow_leading_zeroes: true
  21. })) {
  22. var digits = str.split('').slice(0, -1);
  23. var sum = digits.reduce(function (acc, digit, index) {
  24. return acc + Number(digit) * weightOfDigits[index + 1];
  25. }, 0);
  26. var modulo = sum % 10;
  27. var lastDigit = Number(str.charAt(str.length - 1));
  28. if (modulo === 0 && lastDigit === 0 || lastDigit === 10 - modulo) {
  29. return true;
  30. }
  31. }
  32. return false;
  33. },
  34. ES: function ES(str) {
  35. assertString(str);
  36. var DNI = /^[0-9X-Z][0-9]{7}[TRWAGMYFPDXBNJZSQVHLCKE]$/;
  37. var charsValue = {
  38. X: 0,
  39. Y: 1,
  40. Z: 2
  41. };
  42. var controlDigits = ['T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X', 'B', 'N', 'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C', 'K', 'E']; // sanitize user input
  43. var sanitized = str.trim().toUpperCase(); // validate the data structure
  44. if (!DNI.test(sanitized)) {
  45. return false;
  46. } // validate the control digit
  47. var number = sanitized.slice(0, -1).replace(/[X,Y,Z]/g, function (_char) {
  48. return charsValue[_char];
  49. });
  50. return sanitized.endsWith(controlDigits[number % 23]);
  51. },
  52. FI: function FI(str) {
  53. // https://dvv.fi/en/personal-identity-code#:~:text=control%20character%20for%20a-,personal,-identity%20code%20calculated
  54. assertString(str);
  55. if (str.length !== 11) {
  56. return false;
  57. }
  58. if (!str.match(/^\d{6}[\-A\+]\d{3}[0-9ABCDEFHJKLMNPRSTUVWXY]{1}$/)) {
  59. return false;
  60. }
  61. var checkDigits = '0123456789ABCDEFHJKLMNPRSTUVWXY';
  62. var idAsNumber = parseInt(str.slice(0, 6), 10) * 1000 + parseInt(str.slice(7, 10), 10);
  63. var remainder = idAsNumber % 31;
  64. var checkDigit = checkDigits[remainder];
  65. return checkDigit === str.slice(10, 11);
  66. },
  67. IN: function IN(str) {
  68. var DNI = /^[1-9]\d{3}\s?\d{4}\s?\d{4}$/; // multiplication table
  69. var d = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 0, 6, 7, 8, 9, 5], [2, 3, 4, 0, 1, 7, 8, 9, 5, 6], [3, 4, 0, 1, 2, 8, 9, 5, 6, 7], [4, 0, 1, 2, 3, 9, 5, 6, 7, 8], [5, 9, 8, 7, 6, 0, 4, 3, 2, 1], [6, 5, 9, 8, 7, 1, 0, 4, 3, 2], [7, 6, 5, 9, 8, 2, 1, 0, 4, 3], [8, 7, 6, 5, 9, 3, 2, 1, 0, 4], [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]]; // permutation table
  70. var p = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 7, 6, 2, 8, 3, 0, 9, 4], [5, 8, 0, 3, 7, 9, 6, 1, 4, 2], [8, 9, 1, 6, 0, 4, 3, 5, 2, 7], [9, 4, 5, 3, 1, 2, 6, 8, 7, 0], [4, 2, 8, 6, 5, 7, 3, 9, 0, 1], [2, 7, 9, 3, 8, 0, 6, 4, 1, 5], [7, 0, 4, 6, 9, 1, 3, 2, 5, 8]]; // sanitize user input
  71. var sanitized = str.trim(); // validate the data structure
  72. if (!DNI.test(sanitized)) {
  73. return false;
  74. }
  75. var c = 0;
  76. var invertedArray = sanitized.replace(/\s/g, '').split('').map(Number).reverse();
  77. invertedArray.forEach(function (val, i) {
  78. c = d[c][p[i % 8][val]];
  79. });
  80. return c === 0;
  81. },
  82. IR: function IR(str) {
  83. if (!str.match(/^\d{10}$/)) return false;
  84. str = "0000".concat(str).substr(str.length - 6);
  85. if (parseInt(str.substr(3, 6), 10) === 0) return false;
  86. var lastNumber = parseInt(str.substr(9, 1), 10);
  87. var sum = 0;
  88. for (var i = 0; i < 9; i++) {
  89. sum += parseInt(str.substr(i, 1), 10) * (10 - i);
  90. }
  91. sum %= 11;
  92. return sum < 2 && lastNumber === sum || sum >= 2 && lastNumber === 11 - sum;
  93. },
  94. IT: function IT(str) {
  95. if (str.length !== 9) return false;
  96. if (str === 'CA00000AA') return false; // https://it.wikipedia.org/wiki/Carta_d%27identit%C3%A0_elettronica_italiana
  97. return str.search(/C[A-Z][0-9]{5}[A-Z]{2}/i) > -1;
  98. },
  99. NO: function NO(str) {
  100. var sanitized = str.trim();
  101. if (isNaN(Number(sanitized))) return false;
  102. if (sanitized.length !== 11) return false;
  103. if (sanitized === '00000000000') return false; // https://no.wikipedia.org/wiki/F%C3%B8dselsnummer
  104. var f = sanitized.split('').map(Number);
  105. var k1 = (11 - (3 * f[0] + 7 * f[1] + 6 * f[2] + 1 * f[3] + 8 * f[4] + 9 * f[5] + 4 * f[6] + 5 * f[7] + 2 * f[8]) % 11) % 11;
  106. var k2 = (11 - (5 * f[0] + 4 * f[1] + 3 * f[2] + 2 * f[3] + 7 * f[4] + 6 * f[5] + 5 * f[6] + 4 * f[7] + 3 * f[8] + 2 * k1) % 11) % 11;
  107. if (k1 !== f[9] || k2 !== f[10]) return false;
  108. return true;
  109. },
  110. TH: function TH(str) {
  111. if (!str.match(/^[1-8]\d{12}$/)) return false; // validate check digit
  112. var sum = 0;
  113. for (var i = 0; i < 12; i++) {
  114. sum += parseInt(str[i], 10) * (13 - i);
  115. }
  116. return str[12] === ((11 - sum % 11) % 10).toString();
  117. },
  118. LK: function LK(str) {
  119. var old_nic = /^[1-9]\d{8}[vx]$/i;
  120. var new_nic = /^[1-9]\d{11}$/i;
  121. if (str.length === 10 && old_nic.test(str)) return true;else if (str.length === 12 && new_nic.test(str)) return true;
  122. return false;
  123. },
  124. 'he-IL': function heIL(str) {
  125. var DNI = /^\d{9}$/; // sanitize user input
  126. var sanitized = str.trim(); // validate the data structure
  127. if (!DNI.test(sanitized)) {
  128. return false;
  129. }
  130. var id = sanitized;
  131. var sum = 0,
  132. incNum;
  133. for (var i = 0; i < id.length; i++) {
  134. incNum = Number(id[i]) * (i % 2 + 1); // Multiply number by 1 or 2
  135. sum += incNum > 9 ? incNum - 9 : incNum; // Sum the digits up and add to total
  136. }
  137. return sum % 10 === 0;
  138. },
  139. 'ar-LY': function arLY(str) {
  140. // Libya National Identity Number NIN is 12 digits, the first digit is either 1 or 2
  141. var NIN = /^(1|2)\d{11}$/; // sanitize user input
  142. var sanitized = str.trim(); // validate the data structure
  143. if (!NIN.test(sanitized)) {
  144. return false;
  145. }
  146. return true;
  147. },
  148. 'ar-TN': function arTN(str) {
  149. var DNI = /^\d{8}$/; // sanitize user input
  150. var sanitized = str.trim(); // validate the data structure
  151. if (!DNI.test(sanitized)) {
  152. return false;
  153. }
  154. return true;
  155. },
  156. 'zh-CN': function zhCN(str) {
  157. var provincesAndCities = ['11', // 北京
  158. '12', // 天津
  159. '13', // 河北
  160. '14', // 山西
  161. '15', // 内蒙古
  162. '21', // 辽宁
  163. '22', // 吉林
  164. '23', // 黑龙江
  165. '31', // 上海
  166. '32', // 江苏
  167. '33', // 浙江
  168. '34', // 安徽
  169. '35', // 福建
  170. '36', // 江西
  171. '37', // 山东
  172. '41', // 河南
  173. '42', // 湖北
  174. '43', // 湖南
  175. '44', // 广东
  176. '45', // 广西
  177. '46', // 海南
  178. '50', // 重庆
  179. '51', // 四川
  180. '52', // 贵州
  181. '53', // 云南
  182. '54', // 西藏
  183. '61', // 陕西
  184. '62', // 甘肃
  185. '63', // 青海
  186. '64', // 宁夏
  187. '65', // 新疆
  188. '71', // 台湾
  189. '81', // 香港
  190. '82', // 澳门
  191. '91' // 国外
  192. ];
  193. var powers = ['7', '9', '10', '5', '8', '4', '2', '1', '6', '3', '7', '9', '10', '5', '8', '4', '2'];
  194. var parityBit = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
  195. var checkAddressCode = function checkAddressCode(addressCode) {
  196. return provincesAndCities.includes(addressCode);
  197. };
  198. var checkBirthDayCode = function checkBirthDayCode(birDayCode) {
  199. var yyyy = parseInt(birDayCode.substring(0, 4), 10);
  200. var mm = parseInt(birDayCode.substring(4, 6), 10);
  201. var dd = parseInt(birDayCode.substring(6), 10);
  202. var xdata = new Date(yyyy, mm - 1, dd);
  203. if (xdata > new Date()) {
  204. return false; // eslint-disable-next-line max-len
  205. } else if (xdata.getFullYear() === yyyy && xdata.getMonth() === mm - 1 && xdata.getDate() === dd) {
  206. return true;
  207. }
  208. return false;
  209. };
  210. var getParityBit = function getParityBit(idCardNo) {
  211. var id17 = idCardNo.substring(0, 17);
  212. var power = 0;
  213. for (var i = 0; i < 17; i++) {
  214. power += parseInt(id17.charAt(i), 10) * parseInt(powers[i], 10);
  215. }
  216. var mod = power % 11;
  217. return parityBit[mod];
  218. };
  219. var checkParityBit = function checkParityBit(idCardNo) {
  220. return getParityBit(idCardNo) === idCardNo.charAt(17).toUpperCase();
  221. };
  222. var check15IdCardNo = function check15IdCardNo(idCardNo) {
  223. var check = /^[1-9]\d{7}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}$/.test(idCardNo);
  224. if (!check) return false;
  225. var addressCode = idCardNo.substring(0, 2);
  226. check = checkAddressCode(addressCode);
  227. if (!check) return false;
  228. var birDayCode = "19".concat(idCardNo.substring(6, 12));
  229. check = checkBirthDayCode(birDayCode);
  230. if (!check) return false;
  231. return true;
  232. };
  233. var check18IdCardNo = function check18IdCardNo(idCardNo) {
  234. var check = /^[1-9]\d{5}[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}(\d|x|X)$/.test(idCardNo);
  235. if (!check) return false;
  236. var addressCode = idCardNo.substring(0, 2);
  237. check = checkAddressCode(addressCode);
  238. if (!check) return false;
  239. var birDayCode = idCardNo.substring(6, 14);
  240. check = checkBirthDayCode(birDayCode);
  241. if (!check) return false;
  242. return checkParityBit(idCardNo);
  243. };
  244. var checkIdCardNo = function checkIdCardNo(idCardNo) {
  245. var check = /^\d{15}|(\d{17}(\d|x|X))$/.test(idCardNo);
  246. if (!check) return false;
  247. if (idCardNo.length === 15) {
  248. return check15IdCardNo(idCardNo);
  249. }
  250. return check18IdCardNo(idCardNo);
  251. };
  252. return checkIdCardNo(str);
  253. },
  254. 'zh-TW': function zhTW(str) {
  255. var ALPHABET_CODES = {
  256. A: 10,
  257. B: 11,
  258. C: 12,
  259. D: 13,
  260. E: 14,
  261. F: 15,
  262. G: 16,
  263. H: 17,
  264. I: 34,
  265. J: 18,
  266. K: 19,
  267. L: 20,
  268. M: 21,
  269. N: 22,
  270. O: 35,
  271. P: 23,
  272. Q: 24,
  273. R: 25,
  274. S: 26,
  275. T: 27,
  276. U: 28,
  277. V: 29,
  278. W: 32,
  279. X: 30,
  280. Y: 31,
  281. Z: 33
  282. };
  283. var sanitized = str.trim().toUpperCase();
  284. if (!/^[A-Z][0-9]{9}$/.test(sanitized)) return false;
  285. return Array.from(sanitized).reduce(function (sum, number, index) {
  286. if (index === 0) {
  287. var code = ALPHABET_CODES[number];
  288. return code % 10 * 9 + Math.floor(code / 10);
  289. }
  290. if (index === 9) {
  291. return (10 - sum % 10 - Number(number)) % 10 === 0;
  292. }
  293. return sum + Number(number) * (9 - index);
  294. }, 0);
  295. }
  296. };
  297. export default function isIdentityCard(str, locale) {
  298. assertString(str);
  299. if (locale in validators) {
  300. return validators[locale](str);
  301. } else if (locale === 'any') {
  302. for (var key in validators) {
  303. // https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
  304. // istanbul ignore else
  305. if (validators.hasOwnProperty(key)) {
  306. var validator = validators[key];
  307. if (validator(str)) {
  308. return true;
  309. }
  310. }
  311. }
  312. return false;
  313. }
  314. throw new Error("Invalid locale '".concat(locale, "'"));
  315. }