程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#身份證驗證小例子

C#身份證驗證小例子

編輯:C#基礎知識

代碼如下:

 private string CheckCidInfo(string cid)
        {
            string[] aCity = new string[] { null, null, null, null, null, null, null, null, null, null, null, "北京", "天津", "河北", "山西", "內蒙古", null, null, null, null, null, "遼寧", "吉林", "黑龍江", null, null, null, null, null, null, null, "上海", "江蘇", "浙江", "安微", "福建", "江西", "山東", null, null, null, "河南", "湖北", "湖南", "廣東", "廣西", "海南", null, null, null, "重慶", "四川", "貴州", "雲南", "西藏", null, null, null, null, null, null, "陝西", "甘肅", "青海", "寧夏", "新疆", null, null, null, null, null, "台灣", null, null, null, null, null, null, null, null, null, "香港", "澳門", null, null, null, null, null, null, null, null, "國外" };
            double iSum = 0;
            System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$");
            System.Text.RegularExpressions.Match mc = rg.Match(cid);
            if (!mc.Success)
            {
                return "";
            }
            cid = cid.ToLower();
            cid = cid.Replace("x", "a");
            if (aCity[int.Parse(cid.Substring(0, 2))] == null)
            {
                return "非法地區";
            }
            try
            {
                DateTime.Parse(cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2));
            }
            catch
            {
                return "非法生日";
            }
            for (int i = 17; i >= 0; i--)
            {
                iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
            }
            if (iSum % 11 != 1)
            {
                return ("非法證號");
            }

            return (aCity[int.Parse(cid.Substring(0, 2))] + "," + cid.Substring(6, 4) + "-" + cid.Substring(10, 2) + "-" + cid.Substring(12, 2) + "," + (int.Parse(cid.Substring(16, 1)) % 2 == 1 ? "男" : "女"));

        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved