Home > Web Front-end > JS Tutorial > js verifies whether the real name matches the ID number_javascript skills

js verifies whether the real name matches the ID number_javascript skills

WBOY
Release: 2016-05-16 15:36:48
Original
2615 people have browsed it

In recent projects, an interface needs to be called for real-name authentication. The price of the real-name authentication interface is a few cents higher than that of SMS. Therefore, the conditions for calling real-name authentication must be strictly controlled, so js is used to verify the authenticity. Name and js verification ID number.

Get to the point

js verifies the real name and uses unicode characters for matching. The length of Chinese names is generally 2-4, so the match is repeated {2, 4} times

1.js verify real name

 var regName =/^[\ue-\ufa]{,}$/;
 if(!regName.test(name)){
  alert('真实姓名填写有误');
  return false;
 }
Copy after login

js verification ID number, China’s ID number, the first-generation ID card number is 15 digits, the second-generation ID card number is 18 digits, the last check digit may be a number or 'X ' or 'x', so there are four possibilities: a. 15 digits b. 18 digits c. 17 digits, the eighteenth digit is 'X' d. 17 digits, the eighteenth digit is 'x' '

2.js verification of ID number

 var regIdNo = /(^\d{}$)|(^\d{}$)|(^\d{}(\d|X|x)$)/;
 if(!regIdNo.test(idNo)){
  alert('身份证号填写有误');
  return false;
 }
Copy after login

PS:15位和18位身份证JS校验实例

一、身份证号码的结构和表示形式

1、号码的结构

根据〖中华人民共和国国家标准GB11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。

2、地址码

表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。

3、出生日期码

表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。

4、顺序码

表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配给女性。

5、校验码

(1)十七位数字本体码加权求和公式

S = Sum(Ai * Wi), i = 0, ... , 16 , 先对前17位数字的权求和
Ai:表示第i位置上的身份证号码数字值
Wi:表示第i位置上的加权因子
7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
(2)计算模

Y = mod(S, 11)

(3)通过模得到对应的校验码

Y: 0 1 2 3 4 5 6 7 8 9 10
校验码: 1 0 X 9 8 7 6 5 4 3 2
也就是说,如果得到余数为1则最后的校验位p应该为对应的0。

15位的号码:

a a b b c c y y m m d d x x s

18位的号码:

a a b b c c y y y y m m d d x x s p

二、地址码

身份证前6位为行政区划数字代码(简称数字码)说明(参考《GB/T 2260-2007 中华人民共和国行政区划代码》):该数字码的编制原则和结构分析,它采用三层六位层次码结构,按层次分别表示我国各省(自治区,直辖市,特别行政区)、市(地区,自治州,盟)、县(自治县、县级市、旗、自治旗、市辖区、林区、特区)。

(1)第1、2位数字:所在省份编码,表示省、自治区、直辖市、特别行政区。
省,直辖市代码表: { 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:"国外"}

(2)第3、4位数字:所在省级市(县)编码,表示市、地区、自治州、盟、直辖市所辖市辖区、县汇总码、省(自治区)直辖县级行政区划汇总码。
——01~20、51~70表示市,直辖市则01表示市区,02以后表示直辖市辖区内的郊县;
——21~50表示地区、自治州、盟;
——90表示省(自治区)直辖县级行政区划汇总码。

(3)第5、6位数字:所在地级县(市)编码,表示县、自治县、县级市、旗、自治旗、市辖区、林区、特区。
——01~20表示市辖区、地区(自治州、盟)辖县级市、市辖特区以及省(自治区)直辖县级行政区划中的县级市,01通常表示辖区汇总码;
——21~80表示县、自治县、旗、自治旗、林区、地区辖特区;
——81~99表示省(自治区)辖县级市。

三、其它

15位身份证号码:
第7、8位为出生年份(两位数,用年份的后两位表示),
第9、10位为出生月份,
第11、12位代表出生日期,
第13、14位为顺序码,这个是随机数,
第15位代表性别,奇数为男,偶数为女。
18位身份证号码:
第7、8、9、10位为出生年份(四位数),
第11、12位为出生月份,
第13、14位代表出生日期,
第15、16位为顺序码,
第17位代表性别,奇数为男,偶数为女。
最后一位为校验位。

四、Js代码实例

var idCardNoUtil = {
 /*省,直辖市代码表*/
 provinceAndCitys: {:"北京",:"天津",:"河北",:"山西",:"内蒙古",:"辽宁",:"吉林",:"黑龙江",
 :"上海",:"江苏",:"浙江",:"安徽",:"福建",:"江西",:"山东",:"河南",:"湖北",:"湖南",:"广东",
 :"广西",:"海南",:"重庆",:"四川",:"贵州",:"云南",:"西藏",:"陕西",:"甘肃",:"青海",:"宁夏",
 :"新疆",:"台湾",:"香港",:"澳门",:"国外"},
 /*每位加权因子*/
 powers: ["","","","","","","","","","","","","","","","",""],
 /*第位校检码*/
 parityBit: ["","","X","","","","","","","",""],
 /*性别*/
 genders: {male:"男",female:"女"},
 /*校验地址码*/
 checkAddressCode: function(addressCode){
  var check = /^[-]\d{}$/.test(addressCode);
  if(!check) return false;
  if(idCardNoUtil.provinceAndCitys[parseInt(addressCode.substring(,))]){
  return true;
  }else{
  return false;
  }
 },
 /*校验日期码*/
 checkBirthDayCode: function(birDayCode){
  var check = /^[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))$/.test(birDayCode);
  if(!check) return false; 
  var yyyy = parseInt(birDayCode.substring(,),);
  var mm = parseInt(birDayCode.substring(,),);
  var dd = parseInt(birDayCode.substring(),);
 var xdata = new Date(yyyy,mm-,dd);
 if(xdata > new Date()){
  return false;//生日不能大于当前日期
 }else if ( ( xdata.getFullYear() == yyyy ) && ( xdata.getMonth () == mm - ) && ( xdata.getDate() == dd ) ){
  return true;
 }else{
  return false;
 }
 },
 /*计算校检码*/
 getParityBit: function(idCardNo){
 var id = idCardNo.substring(,); 
  /*加权 */
  var power = ;
  for(var i=;i<;i++){
  power += parseInt(id.charAt(i),) * parseInt(idCardNoUtil.powers[i]);
  }    
  /*取模*/ 
  var mod = power % ;
  return idCardNoUtil.parityBit[mod];
 },
 /*验证校检码*/
 checkParityBit: function(idCardNo){
  var parityBit = idCardNo.charAt().toUpperCase();
  if(idCardNoUtil.getParityBit(idCardNo) == parityBit){
   return true;
  }else{
   return false;
  }
 },
 /*校验位或位的身份证号码*/
 checkIdCardNo: function(idCardNo){
 //位和位身份证号码的基本校验
 var check = /^\d{}|(\d{}(\d|x|X))$/.test(idCardNo);
 if(!check) return false;
 //判断长度为位或位 
 if(idCardNo.length==){
  return idCardNoUtil.checkIdCardNo(idCardNo);
 }else if(idCardNo.length==){
  return idCardNoUtil.checkIdCardNo(idCardNo);
 }else{
  return false;
 }
 },
 //校验位的身份证号码
 checkIdCardNo: function(idCardNo){
  //位身份证号码的基本校验
  var check = /^[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))\d{}$/.test(idCardNo); 
  if(!check) return false;
  //校验地址码
  var addressCode = idCardNo.substring(,);
  check = idCardNoUtil.checkAddressCode(addressCode);
  if(!check) return false;
  var birDayCode = '' + idCardNo.substring(,);
  //校验日期码
  return idCardNoUtil.checkBirthDayCode(birDayCode);
 },
 //校验位的身份证号码
 checkIdCardNo: function(idCardNo){
  //位身份证号码的基本格式校验
  var check = /^[-]\d{}[-]\d{}(([-])|([-]))(([-])|([-][-])|([-]))\d{}(\d|x|X)$/.test(idCardNo);
  if(!check) return false;
  //校验地址码
  var addressCode = idCardNo.substring(,);
  check = idCardNoUtil.checkAddressCode(addressCode);
  if(!check) return false;
  //校验日期码
  var birDayCode = idCardNo.substring(,);
  check = idCardNoUtil.checkBirthDayCode(birDayCode);
  if(!check) return false;
  //验证校检码 
  return idCardNoUtil.checkParityBit(idCardNo); 
 },
 formateDateCN: function(day){
  var yyyy =day.substring(,);
  var mm = day.substring(,);
  var dd = day.substring();
  return yyyy + '-' + mm +'-' + dd;
 },
 //获取信息
 getIdCardInfo: function(idCardNo){
  var idCardInfo = {
  gender:"", //性别
  birthday:"" // 出生日期(yyyy-mm-dd)
  };
 if(idCardNo.length==){
  var aday = '' + idCardNo.substring(,);
  idCardInfo.birthday=idCardNoUtil.formateDateCN(aday);
  if(parseInt(idCardNo.charAt())%==){
   idCardInfo.gender=idCardNoUtil.genders.female;
  }else{
   idCardInfo.gender=idCardNoUtil.genders.male;
  }  
 }else if(idCardNo.length==){
  var aday = idCardNo.substring(,);
  idCardInfo.birthday=idCardNoUtil.formateDateCN(aday);
   if(parseInt(idCardNo.charAt())%==){
   idCardInfo.gender=idCardNoUtil.genders.female;
  }else{
   idCardInfo.gender=idCardNoUtil.genders.male;
  } 
 }
 return idCardInfo;
 },
 /*位转位*/
 getId: function(idCardNo){
 if(idCardNo.length==){
  return idCardNo;
 }else if(idCardNo.length==){
  return idCardNo.substring(,) + idCardNo.substring(,); 
 }else{
 return null;
 }
 },
 /*位转位*/
 getId: function(idCardNo){
 if(idCardNo.length==){
  var id = idCardNo.substring(,) + '' + idCardNo.substring();
  var parityBit = idCardNoUtil.getParityBit(id);
  return id + parityBit;
 }else if(idCardNo.length==){
  return idCardNo;
 }else{
 return null;
 }
 }
};
//身份证号码验证 
jQuery.validator.addMethod("idCardNo", function(value, element) { 
 return this.optional(element) || idCardNoUtil.checkIdCardNo(value); 
}, "Please specify a valid ID number."); 
//获取身份证信息 
var idCardInfo = idCardNoUtil.getIdCardInfo(idCardNo); 
alert(idCardInfo.gender + "|" + idCardInfo.birthday); 
Copy after login

Note: When entering and judging whether the same ID card already exists in the database

(1) If you enter a 15-digit ID card: first check whether the 15-digit ID exists. If it does not exist, you need to convert the 15-digit ID card into an 18-digit ID card. If it still does not exist, you can Enter the system.

(2) If you enter an 18-digit ID card: first check whether the 18-digit ID exists. If it does not exist, you need to convert the 18-digit ID card into a 15-digit ID card. If it still does not exist, you can Enter the system.

If you find the corresponding 15-digit ID card, you need to update the 15-digit ID to 18-digit.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template