zoukankan      html  css  js  c++  java
  • 验证身份证号码

    验证身份证号码

    查看代码
      1         /// <summary>
      2         /// 验证身份证号码
      3         /// </summary>
      4         /// <param name="Id"></param>
      5         /// <returns></returns>
      6         public static bool CheckIDCard(string Id)
      7         {
      8             if (Id.Length == 18)
      9             {
     10                 bool check = CheckIDCard18(Id);
     11                 return check;
     12             }
     13             else if (Id.Length == 15)
     14             {
     15                 bool check = CheckIDCard15(Id);
     16                 return check;
     17             }
     18             else
     19             {
     20                 return false;
     21             }
     22         }
     23 
     24         private static bool CheckIDCard18(string Id)
     25         {
     26             long n = 0;
     27             if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
     28             {
     29                 return false;//数字验证
     30             }
     31             string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
     32             if (address.IndexOf(Id.Remove(2)) == -1)
     33             {
     34                 return false;//省份验证
     35             }
     36             string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
     37             DateTime time = new DateTime();
     38             if (DateTime.TryParse(birth, out time) == false)
     39             {
     40                 return false;//生日验证
     41             }
     42             string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
     43             string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
     44             char[] Ai = Id.Remove(17).ToCharArray();
     45             int sum = 0;
     46             for (int i = 0; i < 17; i++)
     47             {
     48                 sum += int.Parse(Wi) * int.Parse(Ai.ToString());
     49             }
     50             int y = -1;
     51             Math.DivRem(sum, 11, out y);
     52             if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
     53             {
     54                 return false;//校验码验证
     55             }
     56             return true;//符合GB11643-1999标准
     57         }
     58         private static bool CheckIDCard15(string Id)
     59         {
     60             long n = 0;
     61             if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
     62             {
     63                 return false;//数字验证
     64             }
     65             string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
     66             if (address.IndexOf(Id.Remove(2)) == -1)
     67             {
     68                 return false;//省份验证
     69             }
     70             string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
     71             DateTime time = new DateTime();
     72             if (DateTime.TryParse(birth, out time) == false)
     73             {
     74                 return false;//生日验证
     75             }
     76             return true;//符合15位身份证标准
     77         }
     78         /// <summary>
     79         /// 根据身份证号获取生日
     80         /// </summary>
     81         /// <param name="IdCard"></param>
     82         /// <returns></returns>
     83         public static string GetBrithdayFromIdCard(string IdCard)
     84         {
     85             string rtn = "1900-01-01";
     86             if (IdCard.Length == 15)
     87             {
     88                 rtn = IdCard.Substring(6, 6).Insert(4, "-").Insert(2, "-");
     89             }
     90             else if (IdCard.Length == 18)
     91             {
     92                 rtn = IdCard.Substring(6, 8).Insert(6, "-").Insert(4, "-");
     93             }
     94             return rtn;
     95         }
     96         /// <summary>
     97         /// 根据身份证获取性别
     98         /// </summary>
     99         /// <param name="IdCard"></param>
    100         /// <returns></returns>
    101         public static string GetSexFromIdCard(string IdCard)
    102         {
    103             string rtn;
    104             string tmp = "";
    105             if (IdCard.Length == 15)
    106             {
    107                 tmp = IdCard.Substring(IdCard.Length - 3);
    108             }
    109             else if (IdCard.Length == 18)
    110             {
    111                 tmp = IdCard.Substring(IdCard.Length - 4);
    112                 tmp = tmp.Substring(0, 3);
    113             }
    114             int sx = int.Parse(tmp);
    115             int outNum;
    116             Math.DivRem(sx, 2, out outNum);
    117             if (outNum == 0)
    118             {
    119                 rtn = "";
    120             }
    121             else
    122             {
    123                 rtn = "";
    124             }
    125             return rtn;
    126         }
  • 相关阅读:
    Go基础篇【第2篇】: 内置库模块 fmt
    Go基础篇【第1篇】: 内置库模块 OS
    JavaScript学习基础篇【第1篇】: JavaScript 入门
    Python基础篇【第8篇】: Socket编程(二)SocketServer
    Python基础篇【第7篇】: 面向对象(1)
    APP爬虫之Appium使用
    python操作MongoDB
    MySQL性能调优
    ubuntu16.04中启动anaconda图形化界面
    linux 在命令行中通过conda使用anaconda
  • 原文地址:https://www.cnblogs.com/star8521/p/2744331.html
Copyright © 2011-2022 走看看