• 中国身份证号码验证,支持15,18位,可验证成功90%的身份证号


    [csharp] view plain copy
     
    1. <strong class="k">using</strong> System;  
    2.   
    3. <strong class="c">/// <summary>  
    4. </strong><strong class="c">/// Summary description for IDCardValid  
    5. </strong><strong class="c">/// </summary>  
    6. </strong><strong class="k">public</strong> <strong class="k">class</strong> IDCardValid  
    7. {  
    8.     <strong class="k">public</strong> IDCardValid()  
    9.     {  
    10.         <strong class="c">//  
    11. </strong>        <strong class="c">// TODO: Add constructor logic here  
    12. </strong>        <strong class="c">//  
    13. </strong>    }  
    14.   
    15.     <strong class="c">/// <summary>  
    16. </strong>    <strong class="c">/// 验证身份证号码  
    17. </strong>    <strong class="c">/// </summary>  
    18. </strong>    <strong class="c">/// <param name="Id">身份证号码</param>  
    19. </strong>    <strong class="c">/// <returns>验证成功为True,否则为False</returns>  
    20. </strong>    <strong class="k">public</strong> <strong class="k">static</strong> <strong class="k">bool</strong> CheckIDCard(<strong class="k">string</strong> Id)  
    21.     {  
    22.         <strong class="k">if</strong> (Id.Length == 18)  
    23.         {  
    24.             <strong class="k">bool</strong> check = CheckIDCard18(Id);  
    25.             <strong class="k">return</strong> check;  
    26.         }  
    27.         <strong class="k">else</strong> <strong class="k">if</strong> (Id.Length == 15)  
    28.         {  
    29.             <strong class="k">bool</strong> check = CheckIDCard15(Id);  
    30.             <strong class="k">return</strong> check;  
    31.         }  
    32.         <strong class="k">else</strong>  
    33.         {  
    34.             <strong class="k">return</strong> <strong class="k">false</strong>;  
    35.         }  
    36.     }  
    37. <strong class="r">    #region 身份证号码验证  
    38. </strong>  
    39.     <strong class="c">/// <summary>  
    40. </strong>    <strong class="c">/// 验证15位身份证号  
    41. </strong>    <strong class="c">/// </summary>  
    42. </strong>    <strong class="c">/// <param name="Id">身份证号</param>  
    43. </strong>    <strong class="c">/// <returns>验证成功为True,否则为False</returns>  
    44. </strong>    <strong class="k">private</strong> <strong class="k">static</strong> <strong class="k">bool</strong> CheckIDCard18(<strong class="k">string</strong> Id)  
    45.     {  
    46.         <strong class="k">long</strong> n = 0;  
    47.         <strong class="k">if</strong> (<strong class="k">long</strong>.TryParse(Id.Remove(17), <strong class="k">out</strong> n) == <strong class="k">false</strong> || n < Math.Pow(10, 16) || <strong class="k">long</strong>.TryParse(Id.Replace(<strong class="s">'x'</strong>, <strong class="s">'0'</strong>).Replace(<strong class="s">'X'</strong>, <strong class="s">'0'</strong>), <strong class="k">out</strong> n) == <strong class="k">false</strong>)  
    48.         {  
    49.             <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">//数字验证  
    50. </strong>        }  
    51.         <strong class="k">string</strong> address = <strong class="s">"11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"</strong>;  
    52.         <strong class="k">if</strong> (address.IndexOf(Id.Remove(2)) == -1)  
    53.         {  
    54.             <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">//省份验证  
    55. </strong>        }  
    56.         <strong class="k">string</strong> birth = Id.Substring(6, 8).Insert(6, <strong class="s">"-"</strong>).Insert(4, <strong class="s">"-"</strong>);  
    57.         DateTime time = <strong class="k">new</strong> DateTime();  
    58.         <strong class="k">if</strong> (DateTime.TryParse(birth, <strong class="k">out</strong> time) == <strong class="k">false</strong>)  
    59.         {  
    60.             <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">//生日验证  
    61. </strong>        }  
    62.         <strong class="k">string</strong>[] arrVarifyCode = (<strong class="s">"1,0,x,9,8,7,6,5,4,3,2"</strong>).Split(<strong class="s">','</strong>);  
    63.         <strong class="k">string</strong>[] Wi = (<strong class="s">"7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2"</strong>).Split(<strong class="s">','</strong>);  
    64.         <strong class="k">char</strong>[] Ai = Id.Remove(17).ToCharArray();  
    65.         <strong class="k">int</strong> sum = 0;  
    66.         <strong class="k">for</strong> (<strong class="k">int</strong> i = 0; i < 17; i++)  
    67.         {  
    68.             sum += <strong class="k">int</strong>.Parse(Wi[i]) * <strong class="k">int</strong>.Parse(Ai[i].ToString());  
    69.         }  
    70.         <strong class="k">int</strong> y = -1;  
    71.         Math.DivRem(sum, 11, <strong class="k">out</strong> y);  
    72.         <strong class="k">if</strong> (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())  
    73.         {  
    74.             <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">//校验码验证  
    75. </strong>        }  
    76.         <strong class="k">return</strong> <strong class="k">true</strong>;<strong class="c">//符合GB11643-1999标准  
    77. </strong>    }  
    78.   
    79.     <strong class="c">/// <summary>  
    80. </strong>    <strong class="c">/// 验证18位身份证号  
    81. </strong>    <strong class="c">/// </summary>  
    82. </strong>    <strong class="c">/// <param name="Id">身份证号</param>  
    83. </strong>    <strong class="c">/// <returns>验证成功为True,否则为False</returns>  
    84. </strong>    <strong class="k">private</strong> <strong class="k">static</strong> <strong class="k">bool</strong> CheckIDCard15(<strong class="k">string</strong> Id)  
    85.     {  
    86.         <strong class="k">long</strong> n = 0;  
    87.         <strong class="k">if</strong> (<strong class="k">long</strong>.TryParse(Id, <strong class="k">out</strong> n) == <strong class="k">false</strong> || n < Math.Pow(10, 14))  
    88.         {  
    89.             <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">//数字验证  
    90. </strong>        }  
    91.         <strong class="k">string</strong> address = <strong class="s">"11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"</strong>;  
    92.         <strong class="k">if</strong> (address.IndexOf(Id.Remove(2)) == -1)  
    93.         {  
    94.             <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">//省份验证  
    95. </strong>        }  
    96.         <strong class="k">string</strong> birth = Id.Substring(6, 6).Insert(4, <strong class="s">"-"</strong>).Insert(2, <strong class="s">"-"</strong>);  
    97.         DateTime time = <strong class="k">new</strong> DateTime();  
    98.         <strong class="k">if</strong> (DateTime.TryParse(birth, <strong class="k">out</strong> time) == <strong class="k">false</strong>)  
    99.         {  
    100.             <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">//生日验证  
    101. </strong>        }  
    102.         <strong class="k">return</strong> <strong class="k">true</strong>;<strong class="c">//符合15位身份证标准  
    103. </strong>    }  
    104. <strong class="r">    #endregion  
    105. </strong>}  
  • 相关阅读:
    浅谈Dotnet的数据定位和匹配
    聊聊Dotnet的垃圾回收
    Dotnet中Span, Memory和ReadOnlySequence之浅见
    Dotnet的局部函数和委托的对比
    一文说通Dotnet的委托
    开发进阶:Dotnet Core多路径异步终止
    冷饭新炒:理解布隆过滤器算法的实现原理
    冷饭新炒:理解JWT的实现原理和基本使用
    冷饭新炒:理解JDK中UUID的底层实现
    起飞,会了这4个 Intellij IDEA 调试魔法,阅读源码都简单了
  • 原文地址:https://www.cnblogs.com/accumulater/p/6098868.html
Copyright © 2020-2023  润新知