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!');
};


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

相关文章推荐

How To Change the Partition Column Of A Partitioned Table Using DBMS_Redefinition [ID 846405.1]

 How To Change the Partition Column Of A Partitioned Table Using DBMS_Redefinition [ID 846405.1] Mod...

Using Class of Secure Transport (COST) to Restrict Instance Registration in Oracle RAC (Doc ID 13408

Applies to: Oracle Net Services - Version 10.2.0.3 to 11.2.0.4 [Release 10.2 to 11.2] Information...

tomcat启动慢, Creation of SecureRandom instance for session ID generation using [SHA1PRNG]took [xx] mil

tomcat启动慢,熵池阻塞变慢详解, Creation of SecureRandom instance for session ID generation using [SHA1PRNG]took...

ASM Using OS Files Instead of Real Raw Devices On Windows. [ID 602620.1]

 Windows 平台下,使用OS files 代替raw devices,步骤如下: 1)    If the ASM instance was not created, then create a...

Creation of SecureRandom instance for session ID generation using [SHA1PRNG]took [xx] milliseconds

通常情况下,tomcat启动只要2~3秒钟,突然有一天,tomcat启动非常慢,要花5~6分钟,查了很久,终于在这篇文章找到了解决方案,博主牛人啊。原文参见:http://blog.csdn.net/...
  • trackal
  • trackal
  • 2016年09月20日 16:52
  • 4445

chinese character recognition using Tesseract OCR

I have been using Tesseract 3.0.2 OCR SDK for image text extraction. But if I use chinese text i...
  • yuanya
  • yuanya
  • 2014年08月14日 14:28
  • 3234

Using Random Numbers in CUDA

  • 2013年08月06日 15:01
  • 212KB
  • 下载

How to show Chinese character by using Perl?

You need to usemodule:  Unicode::Map   Where to get it? -http://search.cpan.org/~mschwartz/Unicod...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Validation of Chinese ID Numbers using JavaScript
举报原因:
原因补充:

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