C#中華民國外僑及大陸人士在台居留證檢核(舊式+新式)

  • 6046
  • 0
  • C#
  • 2020-10-21

檢核中華民國外僑及大陸人士在台居留證檢核(舊式+新式)

新式公告:
宣導測試:自 109 年 10 月 1 日起至 110 年 1 月 1 日止。
全面換號:自 110 年 1 月 2 日起核發載有新式統號證件。

/// <summary>
/// 檢核中華民國外僑及大陸人士在台居留證(舊式+新式)
/// </summary>
/// <param name="idNo">身分證</param>
/// <returns></returns>
public bool CheckResidentID(string idNo)
{
    if (idNo == null)
    {
        return false;
    }
    idNo = idNo.ToUpper();
    Regex regex = new Regex(@"^([A-Z])(A|B|C|D|8|9)(\d{8})$");
    Match match = regex.Match(idNo);
    if (!match.Success)
    {
        return false;
    }

    if ("ABCD".IndexOf(match.Groups[2].Value) >= 0)
    {
        //舊式
        return CheckOldResidentID(match.Groups[1].Value, match.Groups[2].Value, match.Groups[3].Value);
    }
    else
    {
        //新式(2021/01/02)正式生效
        return CheckNewResidentID(match.Groups[1].Value, match.Groups[2].Value + match.Groups[3].Value);
    }
}

 

/// <summary>
/// 舊式檢核
/// </summary>
/// <param name="firstLetter">第1碼英文字母(區域碼)</param>
/// <param name="secondLetter">第2碼英文字母(性別碼)</param>
/// <param name="num">第3~9流水號 + 第10碼檢查碼</param>
/// <returns></returns>
private bool CheckOldResidentID(string firstLetter, string secondLetter, string num)
{
    ///建立字母對應表(A~Z)
    ///A=10 B=11 C=12 D=13 E=14 F=15 G=16 H=17 J=18 K=19 L=20 M=21 N=22
    ///P=23 Q=24 R=25 S=26 T=27 U=28 V=29 X=30 Y=31 W=32  Z=33 I=34 O=35 
    string alphabet = "ABCDEFGHJKLMNPQRSTUVXYWZIO";
    string transferIdNo =
        $"{alphabet.IndexOf(firstLetter) + 10}" +
        $"{(alphabet.IndexOf(secondLetter) + 10) % 10}" +
        $"{num}";
    int[] idNoArray = transferIdNo.ToCharArray()
                                  .Select(c => Convert.ToInt32(c.ToString()))
                                  .ToArray();

    int sum = idNoArray[0];
    int[] weight = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 1 };
    for (int i = 0; i < weight.Length; i++)
    {
        sum += weight[i] * idNoArray[i + 1];
    }
    return (sum % 10 == 0);
}

 

/// <summary>
/// 新式檢核
/// </summary>
/// <param name="firstLetter">第1碼英文字母(區域碼)</param>
/// <param name="num">第2碼(性別碼) + 第3~9流水號 + 第10碼檢查碼</param>
/// <returns></returns>
private bool CheckNewResidentID(string firstLetter, string num)
{
    ///建立字母對應表(A~Z)
    ///A=10 B=11 C=12 D=13 E=14 F=15 G=16 H=17 J=18 K=19 L=20 M=21 N=22
    ///P=23 Q=24 R=25 S=26 T=27 U=28 V=29 X=30 Y=31 W=32  Z=33 I=34 O=35 
    string alphabet = "ABCDEFGHJKLMNPQRSTUVXYWZIO";
    string transferIdNo = $"{(alphabet.IndexOf(firstLetter) + 10)}" +
                          $"{num}";
    int[] idNoArray = transferIdNo.ToCharArray()
                                  .Select(c => Convert.ToInt32(c.ToString()))
                                  .ToArray();

    int sum = idNoArray[0];
    int[] weight = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 1 };
    for (int i = 0; i < weight.Length; i++)
    {
        sum += (weight[i] * idNoArray[i + 1]) % 10;
    }
    return (sum % 10 == 0);
}

 

參考資料:
外來人口統一證號格式(新式)
外來人口統一證號格式(舊式)

 

創用 CC 授權條款
本著作由Chenting Weng製作,以創用CC 姓名標示 4.0 國際 授權條款釋出。
This work by Chenting Weng is licensed under a Creative Commons Attribution 4.0 International License.
Based on a work at https://dotblogs.com.tw/chentingw.

部分文章內容會引用到其他網站的簡介或圖片,若有侵犯到您的著作權,請留言告知,本人會儘快移除。
免責聲明:文章屬個人記事使用,僅供參考,若引用文章造成一切損失,本人不承擔任何責任。如有錯誤,歡迎留言告知。

Part of the content of the article will refer to the profile or picture of other websites.
If there is any infringement of your copyright, please leave a message and let me remove it as soon as possible.
Disclaimer:The article is for personal use and is for reference only. I will not bear any responsibility for any loss caused by quoting the article. If there is an error, please leave a message to inform.