The purpose of this paper is to realize a function to judge whether the ID number is valid or not
First, create an ID base class:
/// <summary> ///ID card of mainland China /// </summary> public abstract class ChineseIdBase { /// <summary> ///Verify 15 digit ID number /// </summary> ///< param name = "Id" > ID card number < / param > /// <returns></returns> public abstract bool IsGrant(string id); /// <summary> ///Digital Verification /// </summary> /// <param name="id"></param> /// <returns></returns> protected abstract bool CheckNumber(string id); /// <summary> ///Check the provincial and urban areas, and use the first two digits to judge the identity /// </summary> ///< param name = "code" > comparison code < / param > protected virtual bool CheckAddress(string id) { string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; if (address.IndexOf(id.Remove(2)) == -1) { return false; } return true; } /// <summary> ///Verify birthday for 15 ID cards. Take the 7th-14th character on the ID card to judge /// </summary> ///< param name = "Id" > ID card number < / param > /// <returns></returns> protected abstract bool CheckBirth(string id); }
There are 15 ID cards and 18 ID cards.
First, implement the subclass of 15 bit ID card:
/// <summary> ///Identification card judgment in mainland China. For 15 bit judgment /// /// <see cref="!:https://blog.csdn.net/s1102379635/article/details/7967179"/> /// <see cref="!:https://blog.csdn.net/qq_26545305/article/details/75042982"/> /// ///The first 1 or 2 digits indicate the code of the province (municipality directly under the central government or autonomous region); ///The third and fourth digits indicate the code of the local city (autonomous prefecture); ///The 5th and 6th digits indicate the code of the district (county, Autonomous County, county-level city); ///The 7th to 14th digits indicate: year, month and day of birth; ///The 15th and 16th digits indicate the code of the local police station (issuing authority on the back of ID card); ///The 17th digit indicates gender: odd number indicates male, even number indicates female; ///The 18th digit is the verification code (personal information code) (some people may be x, which represents 10 Roman numerals); /// </summary> public class ChineseId15Bit : ChineseIdBase { /// <summary> ///Verify 15 digit ID number /// </summary> ///< param name = "Id" > ID card number < / param > ///< returns > verify success to True, otherwise false < / returns > public override bool IsGrant(string id) { if (string.IsNullOrWhiteSpace(id)) { return false; } if (id.Length != 15) { return false; } //Digital check if (!this.CheckNumber(id)) { return false; } //Check the provincial and urban areas, and use the first two digits to judge the identity if (!this.CheckAddress(id)) { return false; } //Verify birthday for 15 ID cards. Take the 7th-14th character on the ID card to judge if (!this.CheckBirth(id)) { return false; } return true;//Meet 15 ID card standards } /// <summary> ///Digital Verification /// </summary> /// <param name="id"></param> /// <returns></returns> protected override bool CheckNumber(string id) { if (long.TryParse(id, out long n) == false || n < Math.Pow(10, 14)) { return false;//Digital Verification } return true; } /// <summary> ///Verify birthday for 15 ID cards. Take the 7th-14th character on the ID card to judge /// </summary> /// <param name="code"></param> /// <returns></returns> protected override bool CheckBirth(string id) { string birth = id.Substring(6, 6).Insert(4, "-").Insert(2, "-"); DateTime time = new DateTime(); if (DateTime.TryParse(birth, out time) == false) { return false;//Birthday verification } return true; } }
Then write an 18 digit ID card judgment class:
/// <summary> ///Identification card judgment in mainland China. For 18 Bit judgment /// /// <see cref="!:https://blog.csdn.net/s1102379635/article/details/7967179"/> /// <see cref="!:https://blog.csdn.net/qq_26545305/article/details/75042982"/> /// ///The first 1 or 2 digits indicate the code of the province (municipality directly under the central government or autonomous region); ///The third and fourth digits indicate the code of the local city (autonomous prefecture); ///The 5th and 6th digits indicate the code of the district (county, Autonomous County, county-level city); ///The 7th to 14th digits indicate: year, month and day of birth; ///The 15th and 16th digits indicate the code of the local police station (issuing authority on the back of ID card); ///The 17th digit indicates gender: odd number indicates male, even number indicates female; ///The 18th digit is the verification code (personal information code) (some people may be x, which represents 10 Roman numerals); /// </summary> public class ChineseId18Bit : ChineseIdBase { /// <summary> ///Verify 18 digit ID number /// </summary> ///< param name = "Id" > ID card number < / param > ///< returns > verify success to True, otherwise false < / returns > public override bool IsGrant(string id) { if (string.IsNullOrWhiteSpace(id)) { return false; } if (id.Length != 18) { return false; } //Digital check if (!this.CheckNumber(id)) { return false; } //Check the provincial and urban areas, and use the first two digits to judge the identity if (!this.CheckAddress(id)) { return false; } //Birthday verification if (!this.CheckBirth(id)) { return false; } //Judge check code if (!this.CheckVerifyCode(id)) { return false; } return true;//Conform to GB11643-1999 } /// <summary> ///Digital Verification /// </summary> ///< param name = "Id" > ID card number < / param > /// <returns></returns> protected override bool CheckNumber(string id) { if (long.TryParse(id.Remove(17), out long n) == false || n < Math.Pow(10, 16) || long.TryParse(id.Replace('x', '0').Replace('X', '0'), out n) == false) { return false;//Digital Verification } return true; } /// <summary> ///Verify birthday for 15 ID cards. Take the 7th-14th character on the ID card to judge /// </summary> ///< param name = "Id" > ID card number < / param > /// <returns></returns> protected override bool CheckBirth(string id) { string birth = id.Substring(6, 8).Insert(6, "-").Insert(4, "-"); DateTime time = new DateTime(); if (DateTime.TryParse(birth, out time) == false) { return false;//Birthday verification } return true; } /// <summary> ///Inspection code /// </summary> ///< param name = "Id" > ID card number < / param > /// <returns></returns> private bool CheckVerifyCode(string id) { string[] vi = ("1,0,x,9,8,7,6,5,4,3,2").Split(','); string[] wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(','); char[] ai = id.Remove(17).ToCharArray(); int sum = 0; for (int i = 0; i < 17; i++) { sum += int.Parse(wi[i]) * int.Parse(ai[i].ToString()); } int y = -1; Math.DivRem(sum, 11, out y); if (vi[y] != id.Substring(17, 1).ToLower()) { return false;//Verification code verification } return true; } }
Finally, write a management class to call external methods in a unified way. Of course, you can directly instantiate 15 bit or 18 Bit ID judgment classes to implement your own logic without doing so:
/// <summary> ///ID card management /// </summary> public class ChineseIdManager : ITransient { /// <summary> ///Judge whether the ID card passes the verification /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task<bool> IsGrant(string id) { if (string.IsNullOrWhiteSpace(id)) { throw new ArgumentNullException(nameof(id)); } ChineseIdBase checker = null; var len = id.Length; if (len == 15) { checker = new ChineseId15Bit(); } else if (len == 18) { checker = new ChineseId18Bit(); } else { throw new UserFriendlyException("Wrong ID number"); } return await Task.FromResult(checker.IsGrant(id)); } }
How can the client call the above classes and methods
/// <summary> ///Check whether the ID card number has been modified by ps /// </summary> /// <param name="id"></param> /// <returns></returns> private async Task CheckId(string id) { if (!await this._chineseIdManager.IsGrant(id)) { throw new UserFriendlyException("Wrong format of ID card number"); } }
As above, the call and judgment can be realized.