-
中国身份证号码验证,支持15,18位,可验证成功90%的身份证号
- <strong class="k">using</strong> System;
-
- <strong class="c">
- </strong><strong class="c">
- </strong><strong class="c">
- </strong><strong class="k">public</strong> <strong class="k">class</strong> IDCardValid
- {
- <strong class="k">public</strong> IDCardValid()
- {
- <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="c">
- </strong> }
-
- <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="k">public</strong> <strong class="k">static</strong> <strong class="k">bool</strong> CheckIDCard(<strong class="k">string</strong> Id)
- {
- <strong class="k">if</strong> (Id.Length == 18)
- {
- <strong class="k">bool</strong> check = CheckIDCard18(Id);
- <strong class="k">return</strong> check;
- }
- <strong class="k">else</strong> <strong class="k">if</strong> (Id.Length == 15)
- {
- <strong class="k">bool</strong> check = CheckIDCard15(Id);
- <strong class="k">return</strong> check;
- }
- <strong class="k">else</strong>
- {
- <strong class="k">return</strong> <strong class="k">false</strong>;
- }
- }
- <strong class="r"> #region 身份证号码验证
- </strong>
- <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="k">private</strong> <strong class="k">static</strong> <strong class="k">bool</strong> CheckIDCard18(<strong class="k">string</strong> Id)
- {
- <strong class="k">long</strong> n = 0;
- <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>)
- {
- <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">
- </strong> }
- <strong class="k">string</strong> address = <strong class="s">"11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"</strong>;
- <strong class="k">if</strong> (address.IndexOf(Id.Remove(2)) == -1)
- {
- <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">
- </strong> }
- <strong class="k">string</strong> birth = Id.Substring(6, 8).Insert(6, <strong class="s">"-"</strong>).Insert(4, <strong class="s">"-"</strong>);
- DateTime time = <strong class="k">new</strong> DateTime();
- <strong class="k">if</strong> (DateTime.TryParse(birth, <strong class="k">out</strong> time) == <strong class="k">false</strong>)
- {
- <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">
- </strong> }
- <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>);
- <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>);
- <strong class="k">char</strong>[] Ai = Id.Remove(17).ToCharArray();
- <strong class="k">int</strong> sum = 0;
- <strong class="k">for</strong> (<strong class="k">int</strong> i = 0; i < 17; i++)
- {
- sum += <strong class="k">int</strong>.Parse(Wi[i]) * <strong class="k">int</strong>.Parse(Ai[i].ToString());
- }
- <strong class="k">int</strong> y = -1;
- Math.DivRem(sum, 11, <strong class="k">out</strong> y);
- <strong class="k">if</strong> (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
- {
- <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">
- </strong> }
- <strong class="k">return</strong> <strong class="k">true</strong>;<strong class="c">
- </strong> }
-
- <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="c">
- </strong> <strong class="k">private</strong> <strong class="k">static</strong> <strong class="k">bool</strong> CheckIDCard15(<strong class="k">string</strong> Id)
- {
- <strong class="k">long</strong> n = 0;
- <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))
- {
- <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">
- </strong> }
- <strong class="k">string</strong> address = <strong class="s">"11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"</strong>;
- <strong class="k">if</strong> (address.IndexOf(Id.Remove(2)) == -1)
- {
- <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">
- </strong> }
- <strong class="k">string</strong> birth = Id.Substring(6, 6).Insert(4, <strong class="s">"-"</strong>).Insert(2, <strong class="s">"-"</strong>);
- DateTime time = <strong class="k">new</strong> DateTime();
- <strong class="k">if</strong> (DateTime.TryParse(birth, <strong class="k">out</strong> time) == <strong class="k">false</strong>)
- {
- <strong class="k">return</strong> <strong class="k">false</strong>;<strong class="c">
- </strong> }
- <strong class="k">return</strong> <strong class="k">true</strong>;<strong class="c">
- </strong> }
- <strong class="r"> #endregion
- </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
润新知