页面实现代码:
IDCardNumber idCardNumber; public SelectIDCard() { InitializeComponent(); } private void btnQuery_Click(object sender, EventArgs e) { idCardNumber = new IDCardNumber(txtIDCardNumber.Text); lblResult.Text = idCardNumber.CheckIDCardNumber(txtIDCardNumber.Text); }
使用类IDCardNumber源代码:
class IDCardNumber { private string idCardNumber = null; private string areaCode = null; private string areaCodeStr = null; private string yearCode = null; private string monthCode = null; private string dayCode = null; private string orderCode = null; private string checkCode = null; private string result = null; string[] checkCodes ={ "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" }; public IDCardNumber(string idCardNumber) { this.idCardNumber = idCardNumber; } public string CheckIDCardNumber(string idCardNumber) { XmlDocument docXml = new XmlDocument(); docXml.Load(@"AreaCodeInfo.xml"); Regex rg = new Regex(@"^\d{17}(\d|X)$"); Match mc = rg.Match(idCardNumber); if (!mc.Success) { return "非法的身份证号码."; } this.areaCode = idCardNumber.Substring(0, 6); this.yearCode = idCardNumber.Substring(6, 4); this.monthCode = idCardNumber.Substring(10, 2); this.dayCode = idCardNumber.Substring(12, 2); this.orderCode = idCardNumber.Substring(14, 3); this.checkCode = idCardNumber.Substring(17, 1); if (getAreaName(docXml.DocumentElement, areaCode) != null) { result += " 发证地: "; if (getAreaName(docXml.DocumentElement, areaCode.Substring(0, 2).PadRight(6, '0'))!=null) { areaCodeStr += getAreaName(docXml.DocumentElement, areaCode.Substring(0, 2).PadRight(6, '0')); } if (getAreaName(docXml.DocumentElement, areaCode.Substring(0, 4).PadRight(6, '0')) != null) { areaCodeStr += getAreaName(docXml.DocumentElement, areaCode.Substring(0, 4).PadRight(6, '0')); } areaCodeStr += getAreaName(docXml.DocumentElement, areaCode); result += areaCodeStr + "\n"; } else { return "非法的区域代码."; } try { DateTime.Parse(yearCode + "-" + monthCode + "-" + dayCode); result += "出生日期: " + yearCode + "年" + monthCode + "月" + dayCode + "日\n"; } catch { return "非法的出生日期."; } if (getSex()) { result += " 性别: 男\n"; } else { result += " 性别: 女\n"; } if (checkCode != getCheckCode(idCardNumber)) { return "非法的身份证号码."; } return result; } public string getYear() { return yearCode; } public string getAreaCode() { return areaCodeStr; } public string getMonth() { return monthCode; } public string getDay() { return dayCode; } public string getBirthDay() { return yearCode + monthCode + dayCode; } public bool getSex() { if (int.Parse(orderCode) % 2 == 1) { return true; } else { return false; } } public int getAge()//算出年龄 { try { return Convert.ToInt32(DateTime.Now.Year) - Convert.ToInt32(yearCode); } catch { return 0; } } private string getAreaName(XmlNode root, string areaCode) { string result = null; if (root == null) { return null; } if (root is XmlElement) { if (root.Attributes.Count > 0) { if (root.Attributes["code"].Value.Equals(areaCode)) { result = root.Attributes["name"].Value; return result; } } if (root.HasChildNodes) { result = getAreaName(root.FirstChild, areaCode); } if (root.NextSibling != null) { result = getAreaName(root.NextSibling, areaCode); } } return result; } private string getCheckCode(string idCardNumber) { //i----表示号码字符从由至左包括校验码在内的位置序号; //ai----表示第i位置上的号码字符值; //Wi----示第i位置上的加权因子,其数值依据公式Wi=2的(n-1)幂(mod 11)计算得出。 //i 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 //ai 3 4 0 5 2 4 1 9 8 0 0 1 0 1 0 0 1 a1 //Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1 //ai×Wi 21 36 0 25 16 16 2 9 4 8 0 0 9 0 5 0 0 2 a1 //对前17位数字的权求和 double sum = 0; string checkCode = null; for (int i = 2; i <=18; i++) { sum += int.Parse(idCardNumber[18 - i].ToString(), NumberStyles.HexNumber) * (Math.Pow(2, i - 1) % 11); } checkCode = checkCodes[(int)sum % 11]; return checkCode; } }
其中AreaCodeInfo.xml文件:下载 身份证区域数据表还是比较新的,要获取最新的请网上找一下。
实现效果:
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。