使用js验证身份证最后一位字符是否正确

function checkSfzh(sfzh){
		let index_array = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2];
		let sfzh_array  = new Array();
		let total = 0;
		let last_number;

		let start = 0;
		let end = 1;
		for(let i = 0;i < sfzh.length-1;i++){
			let temp = sfzh.slice(start+i,end+i);
			sfzh_array.push(temp);
		}

		for(let i = 0;i < index_array.length;i++){
			let temp = sfzh_array[i]*index_array[i];
			total = total + temp;
		}

		switch(total % 11){
			case 0:
				last_number = 1;
				break;
			case 1:
				last_number = 0;
				break;
			case 2:
				last_number = X;
			case 3:
				last_number = 9;
				break;
			case 4:
				last_number = 8
				break;
			case 5:
				last_number = 7;
				break;
			case 6:
				last_number = 6;
				break;
			case 7:
				last_number = 5;
				break;
			case 8:
				last_number = 4;
				break;
			case 9:
				last_number = 3
				break;
			case 10:
				last_number = 2;
				break;
		}

		if(sfzh.slice(17,18) == last_number){
			return true;
		}else {
			return false;
		}
        
        return true;
}
let sfzh = "321324199507021036";
console.log(checkSfzh(sfzh));

输出结果为false,说明身份证号最后一位不正确。该身份证号有误。

你可能感兴趣的:(js)