鹤壁电销版 自用

idCard.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // ���캯�����Ϊ15λ����18λ�����֤����
  2. function clsIDCard(CardNo) {
  3. this.Valid=false;
  4. this.ID15='';
  5. this.ID18='';
  6. this.Local='';
  7. if(CardNo!=null)this.SetCardNo(CardNo);
  8. }
  9. // �������֤���룬15λ����18λ
  10. clsIDCard.prototype.SetCardNo = function(CardNo) {
  11. this.ID15='';
  12. this.ID18='';
  13. this.Local='';
  14. CardNo=CardNo.replace(" ","");
  15. var strCardNo;
  16. if(CardNo.length==18) {
  17. pattern= /^\d{17}(\d|x|X)$/;
  18. if (pattern.exec(CardNo)==null)return;
  19. strCardNo=CardNo.toUpperCase();
  20. } else {
  21. pattern= /^\d{15}$/;
  22. if (pattern.exec(CardNo)==null)return;
  23. strCardNo=CardNo.substr(0,6)+'19'+CardNo.substr(6,9)
  24. strCardNo+=this.GetVCode(strCardNo);
  25. }
  26. this.Valid=this.CheckValid(strCardNo);
  27. }
  28. // У�����֤��Ч��
  29. clsIDCard.prototype.IsValid = function() {
  30. return this.Valid;
  31. }
  32. // ���������ַ���ʽ���£�1981-10-10
  33. clsIDCard.prototype.GetBirthDate = function() {
  34. var BirthDate='';
  35. if(this.Valid)BirthDate=this.GetBirthYear()+'-'+this.GetBirthMonth()+'-'+this.GetBirthDay();
  36. return BirthDate;
  37. }
  38. // ���������е��꣬��ʽ���£�1981
  39. clsIDCard.prototype.GetBirthYear = function() {
  40. var BirthYear='';
  41. if(this.Valid)BirthYear=this.ID18.substr(6,4);
  42. return BirthYear;
  43. }
  44. // ���������е��£���ʽ���£�10
  45. clsIDCard.prototype.GetBirthMonth = function() {
  46. var BirthMonth='';
  47. if(this.Valid)BirthMonth=this.ID18.substr(10,2);
  48. if(BirthMonth.charAt(0)=='0')BirthMonth=BirthMonth.charAt(1);
  49. return BirthMonth;
  50. }
  51. // ���������е��գ���ʽ���£�10
  52. clsIDCard.prototype.GetBirthDay = function() {
  53. var BirthDay='';
  54. if(this.Valid)BirthDay=this.ID18.substr(12,2);
  55. return BirthDay;
  56. }
  57. // �����Ա�1���У�0��Ů
  58. clsIDCard.prototype.GetSex = function() {
  59. var Sex='';
  60. if(this.Valid)Sex=this.ID18.charAt(16)%2;
  61. return Sex;
  62. }
  63. // ����15λ���֤����
  64. clsIDCard.prototype.Get15 = function() {
  65. var ID15='';
  66. if(this.Valid)ID15=this.ID15;
  67. return ID15;
  68. }
  69. // ����18λ���֤����
  70. clsIDCard.prototype.Get18 = function() {
  71. var ID18='';
  72. if(this.Valid)ID18=this.ID18;
  73. return ID18;
  74. }
  75. // ��������ʡ�����磺�Ϻ��С��㽭ʡ
  76. clsIDCard.prototype.GetLocal = function() {
  77. var Local='';
  78. if(this.Valid)Local=this.Local;
  79. return Local;
  80. }
  81. clsIDCard.prototype.GetVCode = function(CardNo17) {
  82. var Wi = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
  83. var Ai = new Array('1','0','X','9','8','7','6','5','4','3','2');
  84. var cardNoSum = 0;
  85. for (var i=0; i<CardNo17.length; i++)cardNoSum+=CardNo17.charAt(i)*Wi[i];
  86. var seq = cardNoSum%11;
  87. return Ai[seq];
  88. }
  89. clsIDCard.prototype.CheckValid = function(CardNo18) {
  90. if(this.GetVCode(CardNo18.substr(0,17))!=CardNo18.charAt(17))return false;
  91. if(!this.IsDate(CardNo18.substr(6,8)))return false;
  92. var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
  93. if(aCity[parseInt(CardNo18.substr(0,2))]==null)return false;
  94. this.ID18=CardNo18;
  95. this.ID15=CardNo18.substr(0,6)+CardNo18.substr(8,9);
  96. this.Local=aCity[parseInt(CardNo18.substr(0,2))];
  97. return true;
  98. }
  99. clsIDCard.prototype.IsDate = function(strDate) {
  100. var r = strDate.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/);
  101. if(r==null)return false;
  102. var d= new Date(r[1], r[2]-1, r[3]);
  103. return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[2]&&d.getDate()==r[3]);
  104. }