2015年3月7日 星期六

Access Excel Function HKID Check Digit (last digit)

This page explains the algorithm of HKID Check Digit (last digit) and provide a custom Access Excel VBA Function to verify the Check Digit. (香港身份証最後號碼)


HKID Check Digit


Hong Kong ID card number has a last digit with bracket called “Check Digit“. Check Digit is a number to verify if the previous number and alphabet are correct. By understanding the algorithm of calculating Check Digit, you can generate a random HKID.


There are two main purposes for understand HKID Check Digit.


First, some online services only offer to local residents, they do not want overseas residents to register their service, therefore in the membership registration page, they ask you to enter a valid HKID number. The verification system cannot check whether the HKID is real, they only check if your Check Digit is valid.


Second, to check whether employee records in the database are correct, if database management system does not have this function.


Algorithm of HKID Check Digit


There are two types of HKID number:


1) Prefix with one alphabet, such as  A123456(7)


2) Prefix with two alphabets, such as AB123456(7)


I will demonstrate the algorithm below using HKID A123456(x)


Step 1


Convert the prefix alphabet to a numeric value. If Prefix contains two alphabets, add 8 to the original converted value of single alphabet, regardless of the first alphabet.


For example, A should convert to 8, then AA (or BA,CA,DA, etc) should be converted to 8+8 = 16


In our example HKID A123456(x), the result is 8












































































































Prefix AlphabetConverted value
A8
B16
C24
D32
E40
F48
G56
H64
I72
J80
K88
L96
M104
N112
O120
P128
Q136
R144
S152
T160
U168
V176
W184
X192
Y200
Z208
xA16
xB24
xC32
xD40
xE48
xF56
xG64
xH72
xI80
xJ88
xK96
xL104
xM112
xN120
xO128
xP136
xQ144
xR152
xS160
xT168
xU176
xV184
xW192
xX200
xY208
xZ216

Step 2


Convert each HKID digit to a number.
















Nth HKID digitmultiplier
1st7
2nd6
3rd5
4th4
5th3
6th2

In our example HKID A123456(x)


1 is the 1st digit, multiply 1 by 7 = 7


2 is the 2nd digit, multiply 2 by 6 = 12


3 x 5 = 15


4 x 4 = 16


5 x 3 = 15


6 x 2 = 12Total =  77


Step 3


Calculate remainder of (Result 1 + Result 2)/11


= (8+77)/11


= 8


Step 4


Check Digit = Deduct 11 by result of Step 3


= 11 – 8


= 3


Now we know HKID A123456 should have 3 in the bracket, that is A123456(3)


If Check Digit = 10, convert to A


If Check Digit = 11, convert to 0


VBA Code of custom Access Excel Function – HKID Check Digit


Public Function wCheckHKID(ID As String) As String
    Dim newIDArr() As Variant
    newID = UCase(Replace(Replace(ID, "(", ""), ")", ""))
    lenNewID = Len(newID)
    IDnoDigit = Left(newID, lenNewID - 1)
    lenIDnoDigit = Len(IDnoDigit)
    For i = 1 To lenIDnoDigit
        ReDim Preserve newIDArr(i)
        newIDArr(i) = Mid(IDnoDigit, i, 1)
    Next i
    If lenIDnoDigit = 7 Then
        checkSum = (Asc(newIDArr(1)) - 64) * 8 + newIDArr(2) * 7 + newIDArr(3) * 6 + newIDArr(4) * 5 + newIDArr(5) * 4 + newIDArr(6) * 3 + newIDArr(7) * 2
        checkDigit = 11 - checkSum Mod 11
    ElseIf lenIDnoDigit = 8 Then
        checkSum = 6 + (Asc(newIDArr(2)) - 64) * 8 + newIDArr(3) * 7 + newIDArr(4) * 6 + newIDArr(5) * 5 + newIDArr(6) * 4 + newIDArr(7) * 3 + newIDArr(8) * 2
        checkDigit = 11 - checkSum Mod 11
    End If
    If checkDigit = 10 Then
        newCheckdigit = "A"
    ElseIf checkDigit = 11 Then
        newCheckdigit = "0"
    Else: newCheckdigit = checkDigit
    End If
    If Asc(newCheckdigit) = Asc(Right(newID, 1)) Then
        wCheckHKID = True
    Else
        wCheckHKID = False
    End If
End Function


Syntax of custom Access Excel Function – HKID Check Digit


wCheckHKID (ID)



This function removes all brackets and capitalize all alphabets. After the Function calculates the Check Digit, it will compare with the original Check DIgit. This Function returns TRUE if matched, FALSE if not matched.



Access Excel Function HKID Check Digit (last digit)
IDFull HKID such as Z123456(2), accept values with or without brackets, lower case or upper case

沒有留言:

張貼留言