validateIdent.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const validateIdent = {
  2. aIdentityCode_City: { // 城市代码列表
  3. 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林",
  4. 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西",
  5. 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南",
  6. 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃",
  7. 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 "
  8. },
  9. IdentityCode_isCardNo(card) {//检查号码是否符合规范,包括长度,类型
  10. var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/; //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
  11. if (reg.test(card) === false) {
  12. return false;
  13. }
  14. return true;
  15. },
  16. IdentityCode_checkProvince(card) { //取身份证前两位,校验省份
  17. var province = card.substr(0, 2);
  18. if (validateIdent.aIdentityCode_City[province] == undefined) {
  19. return false;
  20. }
  21. return true;
  22. },
  23. IdentityCode_checkBirthday(card) { //检查生日是否正确,15位以'19'年份来进行补齐。
  24. var len = card.length;
  25. //身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字
  26. if (len == '15') {
  27. var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
  28. var arr_data = card.match(re_fifteen); // 正则取号码内所含出年月日数据
  29. var year = arr_data[2];
  30. var month = arr_data[3];
  31. var day = arr_data[4];
  32. var birthday = new Date('19' + year + '/' + month + '/' + day);
  33. return validateIdent.IdentityCode_verifyBirthday('19' + year, month, day, birthday);
  34. }
  35. //身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X
  36. if (len == '18') {
  37. var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
  38. var arr_data = card.match(re_eighteen); // 正则取号码内所含出年月日数据
  39. var year = arr_data[2];
  40. var month = arr_data[3];
  41. var day = arr_data[4];
  42. var birthday = new Date(year + '/' + month + '/' + day);
  43. return validateIdent.IdentityCode_verifyBirthday(year, month, day, birthday);
  44. }
  45. return false;
  46. },
  47. IdentityCode_verifyBirthday(year, month, day, birthday) {//校验日期 ,15位以'19'年份来进行补齐。
  48. var now = new Date();
  49. var now_year = now.getFullYear();
  50. //年月日是否合理
  51. if (birthday.getFullYear() == year
  52. && (birthday.getMonth() + 1) == month
  53. && birthday.getDate() == day) {
  54. //判断年份的范围(3岁到150岁之间)
  55. var time = now_year - year;
  56. if (time >= 3 && time <= 150) {
  57. return true;
  58. }
  59. return false;
  60. }
  61. return false;
  62. },
  63. IdentityCode_checkParity(card) { //校验位的检测
  64. card = validateIdent.IdentityCode_changeFivteenToEighteen(card); // 15位转18位
  65. var len = card.length;
  66. if (len == '18') {
  67. var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  68. var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  69. var cardTemp = 0, i, valnum;
  70. for (i = 0; i < 17; i++) {
  71. cardTemp += card.substr(i, 1) * arrInt[i];
  72. }
  73. valnum = arrCh[cardTemp % 11];
  74. if (valnum == card.substr(17, 1)) {
  75. return true;
  76. }
  77. return false;
  78. }
  79. return false;
  80. },
  81. IdentityCode_changeFivteenToEighteen(card) { //15位转18位身份证号
  82. if (card.length == '15') {
  83. var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  84. var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  85. var cardTemp = 0, i;
  86. card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
  87. for (i = 0; i < 17; i++) {
  88. cardTemp += card.substr(i, 1) * arrInt[i];
  89. }
  90. card += arrCh[cardTemp % 11];
  91. return card;
  92. }
  93. return card;
  94. },
  95. IdentityCodeValid(card) {// 身份证号码检验主入口
  96. let pass = true;
  97. let sex = ''
  98. //是否为空
  99. if (pass && card === '')
  100. pass = false;
  101. //校验长度,类型
  102. if (pass && validateIdent.IdentityCode_isCardNo(card) === false)
  103. pass = false;
  104. //检查省份
  105. if (pass && validateIdent.IdentityCode_checkProvince(card) === false)
  106. pass = false;
  107. //校验生日
  108. if (pass && validateIdent.IdentityCode_checkBirthday(card) === false)
  109. pass = false;
  110. //检验位的检测
  111. if (pass && validateIdent.IdentityCode_checkParity(card) === false)
  112. pass = false;
  113. if (pass) {
  114. var iCard = validateIdent.IdentityCode_changeFivteenToEighteen(card);
  115. if (parseInt(iCard.charAt(16)) % 2 == 0) {
  116. sex = "0"; // 女生
  117. } else {
  118. sex = "1"; // 男生
  119. }
  120. return true
  121. } else {
  122. return false
  123. }
  124. }
  125. }
  126. export default validateIdent.IdentityCodeValid //导出