- <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>}