Yakima Teng

九山八海、为一世界,聚千界则成“小千世界”,此界乘三,无我不断者,三刀流奥义·一大‧三千‧大千‧世界!!!...

Validation of Chinese ID Numbers using JavaScript

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


阅读更多
版权声明:本文为博主原创文章,可随意转载,无需注明作者出处 https://blog.csdn.net/brightyun/article/details/50634567
文章标签: ID
个人分类: javascript
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭