基本信息
源码名称:C# [中华民国身份证字号(第七代)-验证及产生器]
源码大小:0.26M
文件格式:.rar
开发语言:C#
更新时间:2016-08-13
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
TaiwanIdentificationCardCheck [中華民國身分證字號(第七代)-驗證及產生器]

public partial class Form1 : Form
    {
        int D1, D2, D3, D4, D5, D6, D7, D8, D9; //身分證後面9位數字        
        string L1; //身分證第一個字母
        int n1, n2; //身分證第一個字母轉換字元
        string res;
        int iStartIndex;

        int[] id = new int[11];
        string[] address = new string[] { "A.台北市","B.台中市","C.基隆市","D.台南市",
        "E.高雄市","F.新北市","G.宜蘭縣","H.桃園縣","I.嘉義市","J.新竹縣","K.苗栗縣",
        "M.南投縣","N.彰化縣","O.新竹市","P.雲林縣","Q.嘉義縣","T.屏東縣","U.花蓮縣",
        "V.台東縣","W.金門縣","X.澎湖縣","Z.連江縣" };

        private void produce()
        {
            if (comboBox1.Text == address[0]) { id[0] = 10; id[10] = 65; }
            else if (comboBox1.Text == address[1]) { id[0] = 11; id[10] = 66; }
            else if (comboBox1.Text == address[2]) { id[0] = 12; id[10] = 67; }
            else if (comboBox1.Text == address[3]) { id[0] = 13; id[10] = 68; }
            else if (comboBox1.Text == address[4]) { id[0] = 14; id[10] = 69; }
            else if (comboBox1.Text == address[5]) { id[0] = 15; id[10] = 70; }
            else if (comboBox1.Text == address[6]) { id[0] = 16; id[10] = 71; }
            else if (comboBox1.Text == address[7]) { id[0] = 17; id[10] = 72; }
            else if (comboBox1.Text == address[8]) { id[0] = 34; id[10] = 73; }
            else if (comboBox1.Text == address[9]) { id[0] = 18; id[10] = 74; }
            else if (comboBox1.Text == address[10]) { id[0] = 19; id[10] = 75; }
            else if (comboBox1.Text == address[11]) { id[0] = 21; id[10] = 77; }
            else if (comboBox1.Text == address[12]) { id[0] = 22; id[10] = 78; }
            else if (comboBox1.Text == address[13]) { id[0] = 35; id[10] = 79; }
            else if (comboBox1.Text == address[14]) { id[0] = 23; id[10] = 80; }
            else if (comboBox1.Text == address[15]) { id[0] = 24; id[10] = 81; }
            else if (comboBox1.Text == address[16]) { id[0] = 27; id[10] = 84; }
            else if (comboBox1.Text == address[17]) { id[0] = 28; id[10] = 85; }
            else if (comboBox1.Text == address[18]) { id[0] = 29; id[10] = 86; }
            else if (comboBox1.Text == address[19]) { id[0] = 32; id[10] = 87; }
            else if (comboBox1.Text == address[20]) { id[0] = 30; id[10] = 88; }
            else if (comboBox1.Text == address[21]) { id[0] = 33; id[10] = 90; }
        }


        public Form1()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 22; i )
            {
                comboBox1.Items.Add(address[i]);
            }

            //ColorLabel label = new ColorLabel();
            //label.Size = new Size(150, 20);
            //label.Location = new Point(465, 200); ;
            //label.Text = "驗證結果:";
            //label.AppendText(" 格式正確", Color.Red);
            //this.Controls.Add(label);
        }

        private void CheckID(int N)
        {
            if ((N % 10) == 0)
            {
                //MessageBox.Show("格式正確");
                labValid.Text = "驗證結果: 格式正確";
            }
            else
            {
                //MessageBox.Show("格式錯誤");
                labValid.Text = "驗證結果: 格式錯誤";
            }
        }

        /// <summary>
        /// 初次登記的戶籍地編號
        /// </summary>
        /// <param name="value">編號</param>
        /// <returns>戶籍地</returns>
        static string IsCounties(string value)
        {
            string[] stringArray = new string[1];
            switch (value)
            {
                case "A": return "10,臺北市";
                case "B": return "11,臺中市";
                case "C": return "12,基隆市";
                case "D": return "13,臺南市";
                case "E": return "14,高雄市";
                case "F": return "15,新北市";
                case "G": return "16,宜蘭縣";
                case "H": return "17,桃園市";
                case "I": return "34,嘉義市";
                case "J": return "18,新竹縣";
                case "K": return "19,苗栗縣";
                case "M": return "21,南投縣";
                case "N": return "22,彰化縣";
                case "O": return "35,新竹市";
                case "P": return "23,雲林縣";
                case "Q": return "24,嘉義縣";
                case "T": return "27,屏東縣";
                case "U": return "28,花蓮縣";
                case "V": return "29,臺東縣";
                case "W": return "32,金門縣";
                case "X": return "30,澎湖縣";
                case "Z": return "33,連江縣";

                default:
                    return "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ID = textBox1.Text;
            int X2 = 0;
            L1 = ID.Substring(0, 1).ToUpper();//將輸入身分證字號的第一個字母換成大寫
            ID = L1 ID.Substring(1, 9);//取得大寫字母後,再將後面的數字取出來 接在L1後面變成ID
            textBox1.Text = ID;

            //將每一個位數放入D1~D9
            D1 = int.Parse(ID.Substring(1, 1));
            D2 = int.Parse(ID.Substring(2, 1));
            D3 = int.Parse(ID.Substring(3, 1));
            D4 = int.Parse(ID.Substring(4, 1));
            D5 = int.Parse(ID.Substring(5, 1));
            D6 = int.Parse(ID.Substring(6, 1));
            D7 = int.Parse(ID.Substring(7, 1));
            D8 = int.Parse(ID.Substring(8, 1));
            D9 = int.Parse(ID.Substring(9, 1));


            string searchWithinThis = "ABCDEFGHIJKMNOPQTUVWXZ";
            string searchForThis = L1;
            int firstCharacter = searchWithinThis.IndexOf(searchForThis);
            firstCharacter = firstCharacter 1;

            #region 檢查D1(第一個數字),依此判斷性別
            if (D1 == 1)
                tbGender.Text = "男";
            else if (D1 == 2)
                tbGender.Text = "女";
            else
                tbGender.Text = "未知"; 
            #endregion


            #region Method1
            string[] Country = IsCounties(L1).Split(',');

            tbCensusRegister.Text = Country[1];

            var Arr = Country[0].ToCharArray();
            n1 = Convert.ToInt32(Arr[0].ToString());
            n2 = Convert.ToInt32(Arr[1].ToString());

            int sum;
            sum = n1 * 1 n2 * 9 D1 * 8 D2 * 7 D3 * 6 D4 * 5 D5 * 4 D6 * 3 D7 * 2 D8 * 1 D9 * 1; 
            #endregion


            #region Method2
            //將第一個字母所對應的第一位數(X1)和第二位數(X2)做處理
            //int X1=0;
            //if (L1.Equals("A") || L1.Equals("B") || L1.Equals("C") || L1.Equals("D") ||
            //    L1.Equals("E") || L1.Equals("F") || L1.Equals("G") || L1.Equals("H") ||
            //    L1.Equals("I") || L1.Equals("J") || L1.Equals("K"))
            //    X1 = 1;
            //else if (L1.Equals("L") || L1.Equals("M") || L1.Equals("N") || L1.Equals("P") ||
            //    L1.Equals("Q") || L1.Equals("R") || L1.Equals("S") || L1.Equals("T") || 
            //    L1.Equals("U") || L1.Equals("V"))
            //    X1 = 2;
            //else if (L1.Equals("X") || L1.Equals("Y") || L1.Equals("W") || L1.Equals("Z") ||
            //    L1.Equals("I") || L1.Equals("O"))
            //    X1 = 3;

            //if (firstCharacter - 1 == 0 || firstCharacter - 1 != 0)
            //{
            //    X2 = firstCharacter - 1;
            //}
            //else if (firstCharacter - 1 == -1)
            //    X2= 0;

            //int sum;
            //sum = X1 9 * X2 8 * D1 7 * D2 6 * D3
            //      5 * D4 4 * D5 3 * D6 2 * D7 D8 D9; 
            #endregion
            CheckID(sum);
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            int rnd, remainder; //remainder為身分證產生時的餘數
            textBox1.Text = "";
            Random rnd1 = new Random();
            rnd = rnd1.Next(0, 100000000); //決定身分證字號A1XXXXXXXX中,8個X的數字

            produce();

            if (radioButton1.Checked == true) { id[1] = 1; } //判斷第二碼點選是男生or女生
            else if (radioButton2.Checked == true) { id[1] = 2; }
            else if (radioButton3.Checked == true) { id[1] = (rnd % 2) 1; }

            for (int i = 2; i <= 9; i )
            {
                id[i] = Convert.ToInt16(rnd.ToString("D8").Substring(i - 2, 1));
                //id[i]=Convert.ToInt16(rnd.ToString().PadLeft(8,'0').Substring(i-2, 1));
                //以上兩種,皆可將未滿8個字元的rnd亂數補至8字元(以0增加)
                //ex:rnd=238則會變成00000238
            }

            remainder = (id[0] / 10 id[0] % 10 * 9 id[1] * 8 id[2] * 7
            id[3] * 6 id[4] * 5 id[5] * 4 id[6] * 3 id[7] * 2
            id[8] * 1 id[9] * 1) % 10;

            if (remainder == 0)
            {
                textBox1.Text = textBox1.Text.Replace(" ", "") id[10].ToString().Replace(id[10].ToString(), Convert.ToChar(id[10]).ToString());
                for (int i = 1; i <= 9; i )
                { textBox1.Text = id[i].ToString().Replace(" ", ""); }
            }
            else if (remainder > 0)
            {
                id[9] = (id[9] (10 - remainder)) % 10; //檢查碼還原

                textBox1.Text = textBox1.Text.Replace(" ", "") id[10].ToString().Replace(id[10].ToString(), Convert.ToChar(id[10]).ToString());

                for (int i = 1; i <= 9; i )
                {
                    //按下身分證產生按鈕,產生的身分證號碼會顯示在textbox上面
                    textBox1.Text = id[i].ToString().Replace(" ", "");
                }
            }
        }
    }