Validation of Chinese ID Numbers using JavaScript

原创 2016年02月04日 12:44:00

In China, most personal identification numbers now consists of 18 digits (sometimes the roman number X is used as the last digit which refers to 10). However, due to some historical reason, it's not uncommon to see people with 15 digits ID numbers.

15-digit Personal ID Number

In 1985, China employed personal ID number system. And at that time, ID numbers consisted of 15 digits.

  • 1st-6th digits are the location code;
  • 7th-8th digits are the year of birth;
  • 9th-10th digits are the month of birth;
  • 11th-12th digits are the day of birth;
  • 13th-15th digits are the sequence code (odd number refers to male, even number refers to female).

18-digit Personal ID Number

Since the year 1999, the year code is revised to be four-digit number instead of previous two-digit number, and a one-digit check code is newly appended to the last position of personal ID number. Therefore, new kind of personal ID number consists of 18 digits, not 15 digits any longer.
  • 1st-6th digits are the location code (1st-2nd digits refer to province, 3rd-4th digits refer to city, and 5th-6th digits refer to county);
  • 7th-14th digits are the 4-digit year code, 2-digit month code, and 2-digit day code;
  • 15th-17th digits are the sequence code (odd number refers to male, even number refers to female);
  • 18th digit is the check code, which can be 0-9 and X (X is the roman number which refers to 10).

Rough Validation of ID Numbers using JavaScript

Just a rough way!
function validateID(numberOrStr) {
	// trim spaces if any at the two ends of ID string,
	var ID = ('' + numberOrStr).replace(/(^\s*)|(\s*$)/g, "");
	if (/^[1-9]{1}[0-9]{16}([0-9]|[xX])$/.test(ID)) {
		if (checkDate(ID)) {
			// turn ID to an array
			var arrID = ID.split('');
			// factors for 1st-17st ID digits
			var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
			// the check code for the 18th digit in the ID number
			var checkCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];
			var sum = 0;
			// if the 18th digit is the letter X (or x), change it to be the digit 10
			if (arrID[17].toLowerCase() == 'x') { arrID[17] = 10 };
			// multiply first 17 ID digits by corresponding factor elements, and sum these results
			for (var i = 16; i >= 0; i--) {
				sum += arrID[i] * factor[i];
			};
			// var remainder = sum % 11;
			return (arrID[17] == checkCode[sum % 11]) ? true : false;
		} else {
			return false;
		};
	} else if (/^[1-9]{1}[0-9]{14}$/.test(ID)) {
		return checkDate(ID);
	} else {
		return false;
	};
	
	function checkDate(ID) {
		var length18Or15 = ID.length;
		if (length18Or15 == 18) {
			var year = ID.substring(6, 10);
			var month = ID.substring(10, 12);
			var day = ID.substring(12, 14);
			// with parseInt, 05 will be 5
			var tempDate = new Date(parseInt(year), parseInt(month)-1, parseInt(day));
			return (tempDate.getFullYear() != parseInt(year) || tempDate.getMonth() != parseInt(month)-1 || tempDate.getDate() != parseInt(day))
				? false
				: true;
		} else if (length18Or15 == 15) {
			var year = ID.substring(6, 8);
			var month = ID.substring(8, 10);
			var day = ID.substring(10, 12);
			// with parseInt, 05 will be 5
			var tempDate = new Date(parseInt(year), parseInt(month)-1, parseInt(day));
			// .getYear() is not recommended for use any longer,
			// so here getFullYear() is used although it may seem not simple enough
			return (('' + tempDate.getFullYear()).substring(2) != parseInt(year) || tempDate.getMonth() != parseInt(month)-1 || tempDate.getDate() != parseInt(day))
				? false
				: true;
		} else {
			console.log('Unknown error!');
			// return;
		};
	};
};

The following is a demo for using the above function:
if (validateID(333333333333333333)) {
	console.log('The ID number seems to be right');
} else {
	console.log('The ID number must be wrong!');
};


版权声明:本文为博主原创文章,可随意转载,无需注明作者出处

软考报名【javascript Validation Framework运行错误】解决方法

若遇到: javascript Validation Framework运行时错误:TypeError:对象不支持"load"属性或方 :任何运行错误都会导致该域验证失败 只要使用浏览器的兼容性视...
  • cai742925624
  • cai742925624
  • 2014年03月16日 11:26
  • 11744

eclipse关闭js validation的方法

今天在做项目的时候,发现eclipse 控制台Building Workspace忽然变得非常慢,点进去看到是在validation 项目webapp下的js文件,我立马Window-Preferen...
  • u012491783
  • u012491783
  • 2017年03月06日 22:24
  • 534

Validation of Chinese ID Numbers using JavaScript

In China, most personal identification numbers now consists of 18 digits (sometimes the roman number...
  • brightyun
  • brightyun
  • 2016年02月04日 12:44
  • 867

JavaScript Validation Framework(JSValidation)用户手册

1. JSValidation是什么?如首页所说,JSValidation是客户端表单验证框架,用在BS系统中,或者简单的网页系统中。表单验证在这些开发中很常见:新用户注册,需要校验某些字段;用户登陆...
  • jenhy
  • jenhy
  • 2008年04月07日 18:32
  • 1494

LeetCode Add Two Numbers 之JavaScript 多种解法

LeetCode Add Two Numbers 之JavaScript 多种解法
  • weixin_36751895
  • weixin_36751895
  • 2017年07月13日 12:45
  • 374

jsValidtion错误 :javascript validation framework 运行时错误 typeError: undefined 任何运行错误

主要检查这几个地方: 1. 把validation-config.xml,validation-framework.js,validation-confi.dtd这三个文件都拷到网站的根目录下,...
  • zjd1023
  • zjd1023
  • 2012年05月18日 19:01
  • 7794

eclipse去掉对js的Validation

解决eclipse每次编译都validation项目中js类库问题
  • quwenzhe
  • quwenzhe
  • 2016年07月12日 18:15
  • 2478

How to enable your windows displaying chinese font/charctar

For Windows Users• Windows XP/server 2003Windows xp/server 2003 can support both s...
  • superuser007
  • superuser007
  • 2010年09月02日 20:10
  • 3119

JS学习(5)----Numbers

1.Numbersjs中的number基本是64位的单精度浮点数 NaN和Infinity都是Number2.Number Methods(1)toString()var n = 123; n.to...
  • lichen_yun
  • lichen_yun
  • 2017年07月18日 09:20
  • 140

LeetCode系列之——2. Add Two Numbers(JavaScript)

Q: 题目链接:Add Two Numbers 先看题目要求: You are given two non-empty linked lists representing two non-neg...
  • u012158998
  • u012158998
  • 2018年01月25日 00:33
  • 28
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Validation of Chinese ID Numbers using JavaScript
举报原因:
原因补充:

(最多只允许输入30个字)