Example of Verifying ID Card and Obtaining Region Function Implemented by JS

  • 2021-07-12 04:22:12
  • OfStack

This paper describes the functions of verifying ID card and obtaining area realized by JS. Share it for your reference, as follows:

The code here can be used to verify the ID number and determine which province and gender it is based on the ID number

Code example:


<head >
  <title></title>
  <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
  <form id="form1" >
  <label>
     ID number: </label><input type="text" id="txtId" />
  <input type="button" value=" Validation " onclick="CheckId()" />
  </form>
</body>
</html>
<script type="text/javascript">
  // Defining an array of locales 
  var CityArray = { 11: " Beijing ", 12: " Tianjin ", 13: " Hebei ", 14: " Shanxi ", 15: " Inner Mongolia ", 21: " Liaoning ", 22: " Jilin ", 23: " Heilongjiang ", 31: " Shanghai ", 32: " Jiangsu ", 33: " Zhejiang ", 34: " Anhui ", 35: " Fujian ", 36: " Jiangxi ", 37: " Shandong ", 41: " Henan ", 42: " Hubei ", 43: " Hunan ", 44: " Guangdong ", 45: " Guangxi ", 46: " Hainan ", 50: " Chongqing ", 51: "4 Chuan ", 52: " Guizhou ", 53: " Yunnan ", 54: " Tibet ", 61: " Shaanxi ", 62: " Gansu ", 63: " Qinghai ", 64: " Ningxia ", 65: " Xinjiang ", 71: " Taiwan ", 81: " Hong Kong ", 82: " Macao ", 91: " Abroad " }
  // Verify ID card and return area, date of birth and gender 
  function CheckIdCard(sId) {
    if (sId.length == 15) {
      sId = sId.replace(/([\d]{6})(\d{9})/, "$119$2x");
    }
    var iSum = 0
    var info = ""
    if (!/^\d{17}(\d|x)$/i.test(sId)) return " Illegal ID number ";
    sId = sId.replace(/x$/i, "a");
    if (CityArray[parseInt(sId.substr(0, 2))] == null) return "Error: Illegal area ";
    sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
    var d = new Date(sBirthday.replace(/-/g, "/"))
    if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return "Error: Illegal birthday ";
    for (var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11)
    if (iSum % 11 != 1) return "Error: Illegal certificate number ";
    return CityArray[parseInt(sId.substr(0, 2))] + "," + sBirthday + "," + (sId.substr(16, 1) % 2 ? " Male " : " Female ")
  }
  // Call the validation method 
  function CheckId() {
    var id = $("#txtId").val();
    if (id != "") {
      alert(CheckIdCard(id));
    }
  }
</script>

PS: Here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg

More readers interested in JavaScript can check out the topics of this site: "JavaScript Regular Expression Skills Encyclopedia", "JavaScript Replacement Operation Skills Summary", "JavaScript Search Algorithm Skills Summary", "JavaScript Data Structure and Algorithm Skills Summary", "JavaScript Traversal Algorithm and Skills Summary", "json Operation Skills Summary in JavaScript", "JavaScript Error and Debugging Skills Summary" and "JavaScript Mathematical Operation Usage Summary"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: