Validasi check digit Kartu Kredit

Berikut adalah code untuk mencek validasi nomor kartu kredit :

Public Function IsValidCreditCardNumber(ByVal sCardNo As String) As Boolean

Const MAX_DIGITS = 20  ‘ actually don’t know any card using more than 16 digits

Dim anDigits(1 To MAX_DIGITS) As Byte
Dim nDigits As Long

Dim ofsCurrentDigit As Long
Dim ofsCurrentCharacter As Long

Dim CurrentCharacter As String

Dim Multiplier As Long
Dim CheckSum As Long
Dim DigitValue As Long

Dim ValidDigits As String

If Len(Trim$(sCardNo)) < 1 Then
Result = False
GoTo Exit_Point
End If

ValidDigits = “0123456789”

For ofsCurrentCharacter = 1 To Len(sCardNo)
CurrentCharacter = Mid$(sCardNo, ofsCurrentCharacter, 1)
If InStr(1, ValidDigits, CurrentCharacter, vbBinaryCompare) Then
nDigits = nDigits + 1
If nDigits > MAX_DIGITS Then
Result = False
GoTo Exit_Point
End If
anDigits(nDigits) = Val(CurrentCharacter)
End If
Next ofsCurrentCharacter

CheckSum = anDigits(nDigits)

For ofsCurrentDigit = nDigits – 1 To 1 Step -1

If Multiplier = 2 Then
Multiplier = 1
Else
Multiplier = 2
End If
DigitValue = anDigits(ofsCurrentDigit) * Multiplier
CheckSum = CheckSum + DigitValue
If DigitValue > 9 Then
CheckSum = CheckSum – 9
End If

Next ofsCurrentDigit

Result = ((CheckSum Mod 10) = 0)

Exit_Point:

IsValidCreditCardNumber = Result
Exit Function

End Function

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: