C#中華民國營利事業統一編號檢核

中華民國營利事業統一編號檢核

public bool CheckUID(string idNo)
{
    if (idNo == null)
    {
        return false;
    }
    Regex regex = new Regex(@"^\d{8}$");
    Match match = regex.Match(idNo);
    if (!match.Success)
    {
        return false;
    }
    int[] idNoArray = idNo.ToCharArray().Select(c => Convert.ToInt32(c.ToString())).ToArray();
    int[] weight = new int[] { 1, 2, 1, 2, 1, 2, 4, 1 };

    int subSum;     //小和
    int sum = 0;    //總和
    int sumFor7 = 1;
    for (int i = 0; i < idNoArray.Length; i++)
    {
        subSum = idNoArray[i] * weight[i];
        sum += (subSum / 10)   //商數
             + (subSum % 10);  //餘數                
    }
    if (idNoArray[6] == 7)
    {
        //若第7碼=7,則會出現兩種數值都算對,因此要特別處理。
        sumFor7 = sum + 1;
    }
    return (sum % 10 == 0) || (sumFor7 % 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.