valid.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. let num = /^[0-9]*$/
  2. let mobile = /^1[3|4|5|7|8|9][0-9]\d{8}$/
  3. let email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
  4. export default {
  5. isNumber(str) {
  6. return num.test(str)
  7. },
  8. isNumber2(str) {
  9. return typeof str === 'number'
  10. },
  11. isMobile(str) {
  12. return mobile.test(str)
  13. },
  14. isEmail(str) {
  15. return email.test(str)
  16. },
  17. idTransfer(sId) {
  18. if (sId.length === 15) {
  19. let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
  20. arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'],
  21. nTemp = 0
  22. sId = sId.substr(0, 6) + '19' + sId.substr(6, sId.length - 6)
  23. for (let i = 0; i < 17; i++) {
  24. nTemp += Number(sId.substr(i, 1)) * arrInt[i]
  25. }
  26. sId += arrCh[nTemp % 11]
  27. }
  28. return sId
  29. },
  30. // 是否是身份证
  31. isIdCard(s) {
  32. let sId = this.idTransfer(s),
  33. aCity = {
  34. 11: '北京',
  35. 12: '天津',
  36. 13: '河北',
  37. 14: '山西',
  38. 15: '内蒙古',
  39. 21: '辽宁',
  40. 22: '吉林',
  41. 23: '黑龙江 ',
  42. 31: '上海',
  43. 32: '江苏',
  44. 33: '浙江',
  45. 34: '安徽',
  46. 35: '福建',
  47. 36: '江西',
  48. 37: '山东',
  49. 41: '河南',
  50. 42: '湖北 ',
  51. 43: '湖南',
  52. 44: '广东',
  53. 45: '广西',
  54. 46: '海南',
  55. 50: '重庆',
  56. 51: '四川',
  57. 52: '贵州',
  58. 53: '云南',
  59. 54: '西藏 ',
  60. 61: '陕西',
  61. 62: '甘肃',
  62. 63: '青海',
  63. 64: '宁夏',
  64. 65: '新疆',
  65. 71: '台湾',
  66. 81: '香港',
  67. 82: '澳门',
  68. 91: '国外 '
  69. }
  70. if (!/^\d{17}(\d|x)$/i.test(sId)) {
  71. return [false, '身份证格式错误']
  72. }
  73. // sId = sId.replace(/x$/i, "a");
  74. if (!aCity[parseInt(sId.substr(0, 2), 10)]) {
  75. return [false, '身份证前两位错误']
  76. }
  77. var sBirthday =
  78. sId.substr(6, 4) +
  79. '-' +
  80. Number(sId.substr(10, 2)) +
  81. '-' +
  82. Number(sId.substr(12, 2)),
  83. d = new Date(sBirthday.replace(/-/g, '/'))
  84. if (sBirthday !== d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()) {
  85. return [false, '非法身份证号']
  86. }
  87. var sMouth = d.getMonth() + 1,
  88. sDate = d.getDate()
  89. sBirthday =
  90. d.getFullYear() +
  91. '-' +
  92. (sMouth > 9 ? sMouth : '0' + sMouth) +
  93. '-' +
  94. (sDate > 9 ? sDate : '0' + sDate)
  95. /* 十八位身份证的校验码 */
  96. // for (let i = 17; i >= 0; i--) {
  97. // iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
  98. // if ( iSum % 11 !== 1 ) {
  99. // return [false,'非法身份证号'];
  100. // }
  101. // }
  102. if (sId.length == 18) {
  103. // ∑(ai×Wi)(mod 11)
  104. // 加权因子
  105. let factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  106. // 校验位
  107. let parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]
  108. let sum = 0
  109. let ai = 0
  110. let wi = 0
  111. for (let i = 0; i < 17; i++) {
  112. ai = Number(sId[i])
  113. wi = factor[i]
  114. sum += ai * wi
  115. }
  116. let last = parity[sum % 11]
  117. if (last != sId[17].toUpperCase()) {
  118. return [false, '非法身份证号']
  119. }
  120. }
  121. return [true, sBirthday, Number(sId.substr(16, 1)) % 2 ? 'MALE' : 'FEMALE']
  122. }
  123. }