17-10-12, 12:21 AM
كاتب الموضوع : AhmedEssawy
كود :
Public Function IsValidCreditCardNumber(ByVal sCardNo _
As String) As Boolean
'Must get rid of "-" and " " characters before calling this
'function
'alternatively, add Replace(sCardNo, "-", "") and
'Replace (sCardNo, " ", "")
'to the beginning of the function
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 Result As Boolean
Dim ValidDigits As String
If Not IsNumeric(sCardNo) Then Exit Function
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