Home >Web Front-end >JS Tutorial >How to use JS to obtain the birthday, age, and gender of the party through the ID number_javascript skills

How to use JS to obtain the birthday, age, and gender of the party through the ID number_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:18:571895browse

The ID card can identify a person's information. Let's introduce how to use js to obtain the age and gender of the person concerned through the ID card number.

<script>
function IdCard(UUserCard,num){
if(num==1){
//获取出生日期
birth=UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
return birth;
}
if(num==2){
//获取性别
if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
//男
return "男";
} else {
//女
return "女";
}
}
if(num==3){
//获取年龄
var myDate = new Date();
var month = myDate.getMonth() + 1;
var day = myDate.getDate();
var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
age++;
}
return age;
}
}
alert (IdCard('142223198503226111',3));
</script>

Let me share with you a piece of code about obtaining a person’s age and gender through their ID number

The code example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>脚本之家</title>
<script type="text/javascript"> 
function discriCard(UUserCard)
{ 
UUserCard.substring(6,10)+"-"+UUserCard.substring(10,12)+"-"+UUserCard.substring(12,14); 
//获取性别 
if(parseInt(UUserCard.substr(16,1))%2==1) 
{ 
alert("男"); 
//是男则执行代码 ... 
} 
else 
{ 
alert("女"); 
//是女则执行代码 ... 
} 
//获取年龄 
var myDate = new Date(); 
var month = myDate.getMonth() + 1; 
var day = myDate.getDate(); 
var age = myDate.getFullYear()-UUserCard.substring(6, 10) - 1; 
if (UUserCard.substring(10,12)<month||UUserCard.substring(10,12)==month&&UUserCard.substring(12,14)<=day) 
{ 
age++; 
} 
alert(age); 
//年龄 age 
} 
window.onload=function()
{
var txt=document.getElementById("txt");
var bt=document.getElementById("bt");
bt.onclick=function(){discriCard(txt.value);}
}
</script>
</head>
<body>
<input type="text" id="txt" />
<input type="button" value="点击获取信息" id="bt" />
</body>
</html> 

The above code implements our requirements. Enter the ID number in the text box, and then click the button to pop up the age and gender.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn