身份證格式驗證(含15位轉換到18位)C#

來源:互聯網
上載者:User
轉換

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 10; i++)
            {
                Console.Write("{0}:    ", i);
                for (int k = 1; k <= i; k++)
                {
                    Console.Write("{0}" + "  ", i * k);
                }
              
              
                Console.WriteLine();
            }
            string str = "620102197811196218";
            CheckCid t = new CheckCid();
            string str1 = t.CheckCidInfo(str);
            Console.Write(str1);
        }

        /// <summary>
        /// 15 to 19
        /// </summary>
        public class IDCard
        {
            // wi =2(n-1)(mod 11)
            int[] wi = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
            // verify digit
            int[] vi = new int[] { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 };
            private int[] ai = new int[18];
            public IDCard()
            {
            }
            //verify
            public string Verify(string idcard)
            {
                string strCard = "";
                if (idcard.Length == 15)
                {
                    strCard = uptoeighteen(idcard);
                }
                if (idcard.Length == 18)
                {
                    strCard = idcard;
                }
                //string verify = idcard.Substring(17, 18);
                //if (verify.Equals(getVerify(idcard))) {
                //return true;
                //}
                return strCard;
            }
            //get verify
            public string getVerify(string eightcardid)
            {
                int remaining = 0;
                if (eightcardid.Length == 18)
                {
                    eightcardid = eightcardid.Substring(0, 17);
                }
                if (eightcardid.Length == 17)
                {
                    int sum = 0;
                    for (int i = 0; i < 17; i++)
                    {
                        string k = eightcardid.Substring(i, 1);
                        ai[i] = int.Parse(k);
                    }
                    for (int i = 0; i < 17; i++)
                    {
                        sum = sum + wi[i] * ai[i];
                    }
                    remaining = sum % 11;
                }
                return remaining == 2 ? "X" : (vi[remaining]).ToString();
            }
            //15 update to 18
            public string uptoeighteen(string fifteencardid)
            {
                string eightcardid = fifteencardid.Substring(0, 6);
                eightcardid = eightcardid + "19";
                eightcardid = eightcardid + fifteencardid.Substring(6, 9);
                eightcardid = eightcardid + getVerify(eightcardid);
                return eightcardid;
            }
        }
        /// <summary>
        /// 檢測
        /// </summary>
        public class CheckCid
        {
        public 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;
            //string info="";
            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?"男":"女"));

        }
        }
    }
}
 

 



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.