Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7174278
  • 博文数量: 3857
  • 博客积分: 6409
  • 博客等级: 准将
  • 技术积分: 15948
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-02 16:48
个人简介

迷彩 潜伏 隐蔽 伪装

文章分类

全部博文(3857)

文章存档

2017年(5)

2016年(63)

2015年(927)

2014年(677)

2013年(807)

2012年(1241)

2011年(67)

2010年(7)

2009年(36)

2008年(28)

分类: Java

2013-10-11 16:04:09


以下为验证身份证号码的js代码,调用方式为:

checktheform(cardid)调用返回布尔类型结果。

点击(此处)折叠或打开

  1. //TODO
  2. var vcity = {
  3.     11 : "北京",
  4.     12 : "天津",
  5.     13 : "河北",
  6.     14 : "山西",
  7.     15 : "内蒙古",
  8.     21 : "辽宁",
  9.     22 : "吉林",
  10.     23 : "黑龙江",
  11.     31 : "上海",
  12.     32 : "江苏",
  13.     33 : "浙江",
  14.     34 : "安徽",
  15.     35 : "福建",
  16.     36 : "江西",
  17.     37 : "山东",
  18.     41 : "河南",
  19.     42 : "湖北",
  20.     43 : "湖南",
  21.     44 : "广东",
  22.     45 : "广西",
  23.     46 : "海南",
  24.     50 : "重庆",
  25.     51 : "四川",
  26.     52 : "贵州",
  27.     53 : "云南",
  28.     54 : "西藏",
  29.     61 : "陕西",
  30.     62 : "甘肃",
  31.     63 : "青海",
  32.     64 : "宁夏",
  33.     65 : "新疆",
  34.     71 : "台湾",
  35.     81 : "香港",
  36.     82 : "澳门",
  37.     91 : "国外"
  38. };

  39. checktheform = function(cardid) {

  40.     //var card = document.getElementById(cardid).value;
  41.     var card = cardid;
  42.     //是否为空
  43.     if (card === '') {
  44.         //alert('请输入身份证号,身份证号不能为空');
  45.         //document.getElementById(cardid).focus;
  46.         return false;
  47.     }
  48.     //校验长度,类型
  49.     if (isCardNo(card) === false) {
  50.         //alert('您输入的身份证号码不正确,请重新输入');
  51.         //document.getElementById(cardid).focus;
  52.         return false;
  53.     }
  54.     //检查省份
  55.     if (checkProvince(card) === false) {
  56.         //alert('您输入的身份证号码不正确,请重新输入');
  57.         //document.getElementById(cardid).focus;
  58.         return false;
  59.     }
  60.     //校验生日
  61.     if (checkBirthday(card) === false) {
  62.         //alert('您输入的身份证号码生日不正确,请重新输入');
  63.         //document.getElementById(cardid).focus();
  64.         return false;
  65.     }
  66.     //检验位的检测
  67.     if (checkParity(card) === false) {
  68.         //alert('您的身份证校验位不正确,请重新输入');
  69.         //document.getElementById(cardid).focus();
  70.         return false;
  71.     }
  72.     return true;
  73. };

  74. //检查号码是否符合规范,包括长度,类型
  75. isCardNo = function(card) {
  76.     //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
  77.     var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
  78.     if (reg.test(card) === false) {
  79.         return false;
  80.     }

  81.     return true;
  82. };

  83. //取身份证前两位,校验省份
  84. checkProvince = function(card) {
  85.     var province = card.substr(0, 2);
  86.     if (vcity[province] == undefined) {
  87.         return false;
  88.     }
  89.     return true;
  90. };

  91. //检查生日是否正确
  92. checkBirthday = function(card) {
  93.     var len = card.length;
  94.     //身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字
  95.     if (len == '15') {
  96.         var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
  97.         var arr_data = card.match(re_fifteen);
  98.         var year = arr_data[2];
  99.         var month = arr_data[3];
  100.         var day = arr_data[4];
  101.         var birthday = new Date('19' + year + '/' + month + '/' + day);
  102.         return verifyBirthday('19' + year, month, day, birthday);
  103.     }
  104.     //身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X
  105.     if (len == '18') {
  106.         var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
  107.         var arr_data = card.match(re_eighteen);
  108.         var year = arr_data[2];
  109.         var month = arr_data[3];
  110.         var day = arr_data[4];
  111.         var birthday = new Date(year + '/' + month + '/' + day);
  112.         return verifyBirthday(year, month, day, birthday);
  113.     }
  114.     return false;
  115. };

  116. //校验日期
  117. verifyBirthday = function(year, month, day, birthday) {
  118.     var now = new Date();
  119.     var now_year = now.getFullYear();
  120.     //年月日是否合理
  121.     if (birthday.getFullYear() == year && (birthday.getMonth() + 1) == month
  122.             && birthday.getDate() == day) {
  123.         //判断年份的范围(3岁到100岁之间)
  124.         var time = now_year - year;
  125.         if (time >= 3 && time <= 100) {
  126.             return true;
  127.         }
  128.         return false;
  129.     }
  130.     return false;
  131. };

  132. //校验位的检测
  133. checkParity = function(card) {
  134.     //15位转18位
  135.     card = changeFivteenToEighteen(card);
  136.     var len = card.length;
  137.     if (len == '18') {
  138.         var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8,
  139.                 4, 2);
  140.         var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3',
  141.                 '2');
  142.         var cardTemp = 0, i, valnum;
  143.         for (i = 0; i < 17; i++) {
  144.             cardTemp += card.substr(i, 1) * arrInt[i];
  145.         }
  146.         valnum = arrCh[cardTemp % 11];
  147.         if (valnum == card.substr(17, 1)) {
  148.             return true;
  149.         }
  150.         return false;
  151.     }
  152.     return false;
  153. };

  154. //15位转18位身份证号
  155. changeFivteenToEighteen = function(card) {
  156.     if (card.length == '15') {
  157.         var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8,
  158.                 4, 2);
  159.         var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3',
  160.                 '2');
  161.         var cardTemp = 0, i;
  162.         card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
  163.         for (i = 0; i < 17; i++) {
  164.             cardTemp += card.substr(i, 1) * arrInt[i];
  165.         }
  166.         card += arrCh[cardTemp % 11];
  167.         return card;
  168.     }
  169.     return card;
  170. };

阅读(418) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~