checkIdCard.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. function checkIdcard(idcard) {
  2. var Errors = new Array(
  3. "ok",
  4. "身份证号码位数错误",
  5. "身份证号码出生日期错误",
  6. "身份证号码校验错误",
  7. "身份证地区错误"
  8. );
  9. var area = {
  10. 11: "北京", 12: "天津", 13: "河北", 14: "山西",
  11. 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江",
  12. 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽",
  13. 35: "福建", 36: "江西", 37: "山东", 41: "河南",
  14. 42: "湖北", 43: "湖南", 44: "广东", 45: "广西",
  15. 46: "海南", 50: "重庆", 51: "四川", 52: "贵州",
  16. 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃",
  17. 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾",
  18. 81: "香港", 82: "澳门", 91: "国外",
  19. }
  20. var idcard, Y, JYM, ereg;
  21. var S, M;
  22. var idcard_array = new Array();
  23. idcard_array = idcard.split("");
  24. //地区检验
  25. if (area[parseInt(idcard.substr(0, 2))] == null) return Errors[4];
  26. //身份号码位数及格式检验
  27. switch (idcard.length) {
  28. case 15:
  29. if ((parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 || ((parseInt(idcard.substr(6, 2)) + 1900) % 100 == 0 && (parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0)) {
  30. ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/; //测试出生日期的合法性
  31. } else {
  32. ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/; //测试出生日期的合法性
  33. }
  34. if (ereg.test(idcard)) return Errors[0];
  35. else return Errors[2];
  36. break;
  37. case 18:
  38. //18位身份号码检测
  39. //出生日期的合法性检查
  40. //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
  41. //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
  42. if (
  43. parseInt(idcard.substr(6, 4)) % 4 == 0
  44. ||
  45. (
  46. parseInt(idcard.substr(6, 4)) % 100 == 0
  47. &&
  48. parseInt(idcard.substr(6, 4)) % 4 == 0
  49. )
  50. ){
  51. ereg = /^[1-9][0-9]{5}[1-2]+[0-9]{3}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/; //闰年出生日期的合法性正则表达式
  52. } else {
  53. ereg = /^[1-9][0-9]{5}[1-2]+[0-9]{3}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/; //平年出生日期的合法性正则表达式
  54. }
  55. // 测试出生日期的合法性
  56. if (ereg.test(idcard)) {
  57. //计算校验位
  58. S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
  59. + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
  60. + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
  61. + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
  62. + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
  63. + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
  64. + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
  65. + parseInt(idcard_array[7]) * 1
  66. + parseInt(idcard_array[8]) * 6
  67. + parseInt(idcard_array[9]) * 3;
  68. Y = S % 11;
  69. M = "F";
  70. JYM = "10X98765432";
  71. M = JYM.substr(Y, 1); //判断校验位
  72. if (M == idcard_array[17]) return Errors[0]; //检测ID的校验位
  73. else return Errors[3];
  74. }else {
  75. return Errors[2];
  76. }
  77. break;
  78. default:
  79. return Errors[1];
  80. break;
  81. }
  82. }
  83. export default{
  84. checkIdcard : checkIdcard
  85. }