This article analyzes the jQuery method of obtaining birth date and gender from ID number. Share it with everyone for your reference, the details are as follows:
1. Foreword:
Today, in a mobile project, according to the requirements of the design draft, users can input their own date of birth. I also seriously used the date type of the html5 form that I just learned about. I thought I would not use the date after all. The plug-in allows users to choose their date of birth, but the boss said that this form should be removed and the user's date of birth should be read from the ID number. Well, half happy, the result is... Alas, I have no choice but to do it according to the leader's request, so I have the code below to obtain the date of birth and gender from the ID number.
2. Implementation code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <script src="jquery.min.js"></script> </head> <body> <input type="tel" id="js_Idcard"> <span id="js_birthday"></span> <script> $(function(){ function GetBirthdatByIdNo(iIdNo){ var tmpStr = ""; var birthday = $("#js_birthday"); iIdNo = $.trim(iIdNo); if(iIdNo.length == 15){ tmpStr = iIdNo.substring(6, 12); tmpStr = "19" + tmpStr; tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6); sexStr = parseInt(iIdNo.substring(14, 1),10) % 2 ? "男" : "女"; birthday.text(sexStr + tmpStr); }else{ tmpStr = iIdNo.substring(6, 14); tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6); sexStr = parseInt(iIdNo.substring(17, 1),10) % 2 ? "男" : "女"; birthday.text(sexStr + tmpStr); } } $("#js_Idcard").blur(function(){ GetBirthdatByIdNo($(this).val()); }); }); </script> </body> </html>
Another code to get gender from ID number:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <script src="jquery.min.js"></script> </head> <body> <input type="tel" id="js_Idcard"> <span id="js_birthday"></span> <script> $(function(){ function go(){ var id = $("#js_Idcard").val(); var last = id[id.length - 2]; if(last % 2 != 0){ $("#js_birthday").text("男"); }else{ $("#js_birthday").text("女"); } } $("#js_Idcard").blur(function(){ go(); }); }); </script> </body> </html>
Readers who are interested in more jQuery-related content can check out the special topics on this site: "JQuery drag effects and skills summary", "jQuery extension skills summary", "JQuery common classic special effects summary", "jQuery animation and special effects usage summary" and "jquery selector usage summary"
I hope this article will be helpful to everyone in jQuery programming.