1. 程式人生 > >通過身份證號動態獲取生日、性別、年齡

通過身份證號動態獲取生日、性別、年齡

$('#idNumber').blur(function (){
		var UUserCard = $("#idNumber").val(); 
		if(UUserCard!=null&&UUserCard.trim()!=""){
			var birthday = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14); 
			$("#birthday").val(birthday);
			$('.date-picker').datepicker('setStartDate',birthday);  
			if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) { 
				$("#gender1").val(1);//男
				$("#gender").val(1);
			} else {
				$("#gender1").val(2);//女
				$("#gender").val(2);
			}
			//獲取年齡 
			var myDate = new Date(); 
			var month = myDate.getMonth() + 1; 
			var day = myDate.getDate();
			var age = myDate.getFullYear() - UUserCard.substring(6, 10); 
	// 		if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
	// 			age++; 
	// 		} 
			$("#age").val(age);
		}
});