JS身份证号获取生日、年龄、性别
在一些身份验证的地方,有些客户会要求当用户输入身份证信息时直接读取他们的年龄和性别,今天就和大家分享下如何使用JS实现通过身份证号码获取人的年龄和性别,有需要的朋友可以直接拿来使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <!DOCTYPE html> < html > < head > < meta charset = "utf-8" > < 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 > |