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

 

————————链接——————————

顾村推荐网

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Validation是一种用于校验数据的框架,它提供了一系列的注解和工具类,用于对数据进行验证。在Java中,有两个常用的校验注解:@Valid和@Validated。这两个注解的功能大部分相似,但也有一些区别。 @Valid注解属于javax.validation包下的注解,而@Validated注解属于Spring框架下的注解。@Valid注解支持嵌套校验,即可以对对象中的属性进行递归校验,而@Validated注解不支持嵌套校验。另外,@Validated注解支持分组校验,可以根据不同的分组对数据进行不同的校验,而@Valid注解不支持分组校验。 在实际业务中,如果需要对数据进行校验,可以使用已有的校验注解,如@NotNull、@Size等。但是有时候这些注解可能无法满足业务需求,这时可以考虑自定义Validation注解。自定义Validation注解可以通过编写自定义的校验器来实现对数据的校验。 使用@Validated注解时,可以在需要校验的Model上加上@Valid注解。同时,可以使用通用的Validator校验工具类来对对象进行校验。该工具类使用了Java的Validator接口来进行校验,可以根据校验结果抛出异常或返回校验结果。 总结起来,Validation是一种用于校验数据的框架,提供了@Valid和@Validated两个注解,可以用于对数据进行校验。@Valid注解属于javax.validation包下的注解,支持嵌套校验;@Validated注解属于Spring框架下的注解,支持分组校验。自定义Validation注解可以通过编写自定义的校验器来实现对数据的校验。可以使用通用的Validator校验工具类来对对象进行校验。 #### 引用[.reference_title] - *1* *2* [SpringBoot使用Validation校验参数](https://blog.csdn.net/justry_deng/article/details/86571671)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Java类属性字段校验(validation的使用)](https://blog.csdn.net/rao991207823/article/details/117001686)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值