Yeah, you would think I would have something better to put here after such a long absence. Still, it's something.
This is nearly identical to the code for the Credit Card Checksum posted earlier. This code does the validation of the checksum for an Ontario health card. I needed it for a project with the Ministry, found my own code on the Internet (!) and hacked it to fit.
I don't know the exact quote, but someone well known in open source said something to the effect that real men don't do backups, they just publish to the Internet. Here's my backup. Cheers,
Bob
'Health Card Validation Routine (Visual Basic)
'
'Copyright (c) Bob Trower, 2001, 2010
'This source code may be used as you wish, provided
'that these comments remain in the code.
'
'Returns True if the card number is valid, False otherwise
'Algorithm is the one specified by the Ontario Ministry
'of Health (in turn similar to the 'LUHN' formula
'specified in ANSI X4.13)
'Last Digit of Card Number is the 'check digit'.
'From right to left, take value of check digit and add:
' double every second number and sum resulting digits.
' add the non-doubled digits.
'If the Health Card Number is valid, the final sum
'will be an even multiple of ten ((x mod 10) = 0 ).
Function IsValidHCNumber(HCNumber As String) As Integer
Dim CheckSum As Integer
Dim ThisDigit As Integer
Dim i As Integer
' Note: Order of Steps is important...
IsValidHCNumber = False ' Redundant in VB, but good
practice
If Len(HCNumber) = 10 Then ' Known Card numbers are 10
digits long
CheckSum = Val(Mid(HCNumber, Len(HCNumber), 1)) 'Last
Digit is CheckSum Digit
For i = Len(HCNumber) - 1 To 1 Step -2
ThisDigit = Val(Mid(HCNumber, i, 1)) * 2 ' Double
these digits
CheckSum = CheckSum + (ThisDigit \ 10) +
(ThisDigit Mod 10) ' Add the sum of the resulting digits
Next
For i = Len(HCNumber) - 2 To 1 Step -2
CheckSum = CheckSum + Val(Mid(HCNumber, i, 1)) '
Add these digits
Next
If CheckSum Mod 10 = 0 Then ' Resulting number should
be an even multiple of 10
IsValidHCNumber = True
End If
End If
End Function
