2026年3月25日 星期三

營利事業統一編號檢查碼邏輯修正

 營利事業統一編號檢查碼邏輯修正

--

舊版的統一編號邏輯檢查已經不適用囉!


趕緊修正一下




   Public Shared Function NEW_ValidateTaxID(ByVal vstrTaxID As String) As Boolean


        '空值或非8碼或非純數字


        If String.IsNullOrEmpty(vstrTaxID) OrElse vstrTaxID.Trim().Length <> 8 OrElse Not Integer.TryParse(vstrTaxID, Nothing) Then


            Return False


        End If




        Dim TaxID_7 As Boolean = (vstrTaxID.Substring(6, 1) = "7")


        Dim logics As Integer() = New Integer() {1, 2, 1, 2, 1, 2, 4, 1} '邏輯乘數


        Dim Z As Integer = 0 '垂直乘積之和 全部加總


        Dim Z1 As Integer = 0


        Dim Z2 As Integer = 0




        '邏輯乘數的數量與統編長度8碼一致


        For i As Integer = 0 To logics.Length - 1


            Dim num As Integer = Convert.ToInt32(vstrTaxID.Substring(i, 1)) '統編單一數字拆解


            Dim j As Integer = num * logics(i) '乘積直寫並上下相加


            Dim SumXX As Integer = (j \ 10) + (j Mod 10) '垂直乘積之和(上下數字相加)




            If TaxID_7 AndAlso i = 6 Then '統編第7位數字是"7" && 走訪到統編第7位數字時


                '"乘積之和"為"10",但政府文件要我們取"0或1"兩種情況(不是10)來相加。


                Z1 += 0


                Z2 += 1


                'Z 不理它


            Else


                Z += SumXX


                Z1 += SumXX


                Z2 += SumXX


            End If


        Next 'end for




        'int old_checkNum = 10; '2023年以前的舊版


        Dim checkNum As Integer = 5




        If TaxID_7 Then


            If (Z1 Mod checkNum = 0) OrElse (Z2 Mod checkNum = 0) Then


                Return True


            End If


        ElseIf Z Mod checkNum = 0 Then


            Return True


        End If




        Return False


    End Function

沒有留言:

張貼留言