驗証碼計算方式
將字母轉換成二位數然後 sum= 十位數+個位數*9
後面 7 碼分別乘上 8,7,6,….1, 相加後再與sum相加
驗証碼即為 10-(sum%10), 但若結果為10, 則驗証碥為0
| 出生縣市 英文代號 檢驗數字 | 台北市 A 10 | 台中市 B 11 | 基隆市 C 12 | 台南市 D 13 | 高雄市 E 14 | 台北縣 F 15 | 
| 出生縣市 英文代號 檢驗數字 | 宜蘭縣 G 16 | 桃園縣 H 17 | 嘉義市 I 34 | 新竹縣 J 18 | 苗栗縣 K 19 | 台中縣 L 20 | 
| 出生縣市 英文代號 檢驗數字 | 南投縣 M 21 | 彰化縣 N 22 | 新竹市 O 35 | 雲林縣 P 23 | 嘉義縣 Q 24 | 台南縣 R 25 | 
| 出生縣市 英文代號 檢驗數字 | 高雄縣 S 26 | 屏東縣 T 27 | 花蓮縣 U 28 | 台東縣 V 29 | 金門縣 W 30 | 澎湖縣 X 31 | 
| 出生縣市 英文代號 檢驗數字 | 陽明山 Y 32 | 連江縣 Z 33 | ||||
身份証驗証碼 A 1 2 3 4 5 6 7 8 9 10 8 7 6 5 4 3 2 1 1+0*9 8 14 18 20 20 18 14 8 = 121 121 % 10 =1 10-1 = 9
完整代碼如下
using System;
using System.Windows;
namespace TaiwanId
{
    public partial class MainWindow : Window
    {
        string[] city = {
            "臺北市","臺中市","基隆市","臺南市","高雄市","臺北縣","宜蘭縣",
            "桃園縣","嘉義市","新竹縣","苗栗縣","臺中縣","南投縣","彰化縣",
            "新竹市","雲林縣","嘉義縣","臺南縣","高雄縣","屏東縣","花蓮縣",
            "臺東縣","金門縣","澎湖縣","陽明山","連江縣" };
        int[] code = {
            10,11,12,13,14,15,16,
            17,34,18,19,20,21,22,
            35,23,24,25,26,27,28,
            29,30,31,32,33};
        string[] alias = {
            "A","B","C","D","E","F","G",
            "H","I","J","K","L","M","N",
            "O","P","Q","R","S","T","U",
            "V","W","X","Y","Z"};
 
        public MainWindow()
        {
            InitializeComponent();
            foreach(string s in city)
            {
                cmbCity.Items.Add(s);
            }
            cmbSex.Items.Add("男");
            cmbSex.Items.Add("女");
            cmbCity.SelectedIndex = 0;
            cmbSex.SelectedIndex = 0;
        }
        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            Random r = new Random();
            string id = "";
            id += alias[cmbCity.SelectedIndex];
            if (cmbSex.SelectedIndex == 0) id += "1";
            else id += "2";
            for (int i = 0; i < 7; i++)
            {
                id += r.Next(0, 10);
            }
            //txtId.Text = id;
            txtId.Text = id + checksum(id);
        }
        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            lblChecksum.Content=checksum(txtId2.Text).ToString();
        }
        private int checksum(string id)
        {
            int sum = 0;
            if (id.Length != 9)
            {
                MessageBox.Show("長度必需為 9 位數");
                return -1;
            }
            char t = id.Substring(0, 1).ToUpper().ToCharArray()[0];
            if (t < 65 || t > 90)
            {
                MessageBox.Show("第一碼必需為英文字母");
                return -1;
            }
            //英文字母轉成2位數字後, 十位數加上個位數*9
            int c = code[t - 65];
            sum = c / 10 + (c % 10) * 9;
            for (int i = 1; i <= 8; i++)
            {
                int x = Int32.Parse(id.Substring(i, 1));
                sum += x * (9 - i);
            }
            return (10 - sum % 10) % 10;
        }
    }
}
xaml 檔如下
<Window x:Class="TaiwanId.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TaiwanId" mc:Ignorable="d" Title="MainWindow" Height="450" Width="860"> <Grid> <Grid.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF8DFFFA" Offset="0"/> <GradientStop Color="#FFEBABEE" Offset="1"/> </LinearGradientBrush> </Grid.Background> <StackPanel Orientation="Vertical"> <StackPanel Orientation="Vertical"> <StackPanel.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFF6FF3E" Offset="0"/> <GradientStop Color="#FF37C6FB" Offset="1"/> </LinearGradientBrush> </StackPanel.Background> <Label Content="台灣身份証字號產生器" HorizontalAlignment="Center" FontSize="24"/> </StackPanel> <StackPanel Orientation="Horizontal" Margin="0,20,0,0"> <Label Content="縣市" FontSize="24" VerticalContentAlignment="Center"/> <ComboBox x:Name="cmbCity" Width="100" FontSize="24" VerticalContentAlignment="Center"/> <Label Content="性別 " Margin="20,0,0,0" FontSize="24" VerticalContentAlignment="Center"/> <ComboBox x:Name="cmbSex" Width="100" FontSize="24" VerticalContentAlignment="Center"/> <Button Content="產生" x:Name="btnNew" Click="btnNew_Click" Margin="20,0,0,0" FontSize="24" Width="100"/> <Label Content="新身份証字號" FontSize="24" Margin="20,0,0,0" VerticalContentAlignment="Center"/> <TextBox x:Name="txtId" Width="200" Height="50" Background="#FF9CF120" FontSize="24" IsReadOnly="True" VerticalContentAlignment="Center" /> </StackPanel> <StackPanel Orientation="Horizontal" Margin="0,20,0,0"> <StackPanel.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF14E7F1" Offset="0"/> <GradientStop Color="#FFB5B8F9" Offset="1"/> </LinearGradientBrush> </StackPanel.Background> <Label Content="身份証前九碼" FontSize="24" VerticalContentAlignment="Center"/> <TextBox x:Name="txtId2" Width="200" FontSize="24" VerticalContentAlignment="Center"/> <Label x:Name="lblChecksum" Width="50" FontSize="24" Background="#FFD1D1D1" VerticalContentAlignment="Center"/> <Button x:Name="btnCheck" Content="檢碼" Height="50" Width="100" FontSize="24" Margin="20,0,0,0" Click="btnCheck_Click" VerticalContentAlignment="Center"/> </StackPanel> </StackPanel> </Grid> </Window>

