CertificateNumberValidator.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. namespace YBEE.EQM.Core;
  3. /// <summary>
  4. /// 证件号码验证器
  5. /// </summary>
  6. public static class CertificateNumberValidator
  7. {
  8. /// <summary>
  9. /// 校验身份证号码
  10. /// </summary>
  11. /// <param name="idNumber"></param>
  12. /// <returns></returns>
  13. public static IdCardValidateResult ValidateIdCard(string idNumber)
  14. {
  15. IdCardValidateResult result = new() { IdNumber = idNumber, Gender = Gender.UNKNOWN, Success = false };
  16. // 数字验证
  17. if (idNumber.Length != 18 || long.TryParse(idNumber.Remove(17), out long n) == false || n < Math.Pow(10, 16) || long.TryParse(idNumber.Replace('x', '0').Replace('X', '0'), out _) == false)
  18. {
  19. result.ErrorMessage = "格式(应为18位数字,或17位数字+X)";
  20. return result;
  21. }
  22. // 省份验证
  23. string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
  24. if (!address.Contains(idNumber.Remove(2), StringComparison.CurrentCulture))
  25. {
  26. result.ErrorMessage = "前2位省份代码";
  27. return result;
  28. }
  29. // 出生日期验证
  30. string birth = idNumber.Substring(6, 8).Insert(6, "-").Insert(4, "-");
  31. if (DateTime.TryParse(birth, out DateTime birthDate) == false)
  32. {
  33. result.ErrorMessage = "第7至14位出生日期";
  34. return result;
  35. }
  36. result.BirthDate = birthDate;
  37. // 校验码验证
  38. string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
  39. string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
  40. char[] Ai = idNumber.Remove(17).ToCharArray();
  41. int sum = 0;
  42. for (int i = 0; i < 17; i++)
  43. {
  44. sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
  45. }
  46. Math.DivRem(sum, 11, out int y);
  47. if (arrVarifyCode[y] != idNumber.Substring(17, 1).ToLower())
  48. {
  49. result.ErrorMessage = "第18位(校验位)";
  50. return result;
  51. }
  52. // 性别
  53. string g = idNumber.Substring(idNumber.Length - 2, 1);
  54. if ("02468".Contains(g))
  55. {
  56. result.Gender = Gender.FEMALE;
  57. }
  58. else
  59. {
  60. result.Gender = Gender.MALE;
  61. }
  62. result.Success = true;
  63. //result.ErrorMessage = "符合GB11643-1999标准";
  64. return result;
  65. }
  66. /// <summary>
  67. /// 身份证号码验证返回结果
  68. /// </summary>
  69. public class IdCardValidateResult
  70. {
  71. /// <summary>
  72. /// 身份证号码
  73. /// </summary>
  74. public string IdNumber { get; set; }
  75. /// <summary>
  76. /// 出生日期
  77. /// </summary>
  78. public DateTime? BirthDate { get; set; }
  79. /// <summary>
  80. /// 性别
  81. /// </summary>
  82. public Gender Gender { get; set; } = Gender.UNKNOWN;
  83. /// <summary>
  84. /// 是否成功
  85. /// </summary>
  86. public bool Success { get; set; } = false;
  87. /// <summary>
  88. /// 错误消息
  89. /// </summary>
  90. public string ErrorMessage { get; set; }
  91. }
  92. }