<strong class="k">using</strong> System;
<strong class="c">/// <summary>
</strong><strong class="c">/// Summary description for IDCardValid
</strong><strong class="c">/// </summary>
</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">// TODO: Add constructor logic here
</strong> <strong class="c">//
</strong> }
<strong class="c">/// <summary>
</strong> <strong class="c">/// 驗證身份證號碼
</strong> <strong class="c">/// </summary>
</strong> <strong class="c">/// <param name="Id">身份證號碼</param>
</strong> <strong class="c">/// <returns>驗證成功為True,否則為False</returns>
</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">/// <summary>
</strong> <strong class="c">/// 驗證15位身份證號
</strong> <strong class="c">/// </summary>
</strong> <strong class="c">/// <param name="Id">身份證號</param>
</strong> <strong class="c">/// <returns>驗證成功為True,否則為False</returns>
</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">//符合GB11643-1999標准
</strong> }
<strong class="c">/// <summary>
</strong> <strong class="c">/// 驗證18位身份證號
</strong> <strong class="c">/// </summary>
</strong> <strong class="c">/// <param name="Id">身份證號</param>
</strong> <strong class="c">/// <returns>驗證成功為True,否則為False</returns>
</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">//符合15位身份證標准
</strong> }
<strong class="r"> #endregion
</strong>}