VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Validate a credit card number. Accepts a credit card number and returns True if the Card Number is

by Daniel S Boucher (4 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 16th May 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Validate a credit card number. Accepts a credit card number and returns True if the Card Number is valid or false if it is not valid.

Rate Validate a credit card number. Accepts a credit card number and returns True if the Card Number is



Function ValidCC(CCNumber As String) As Boolean


'Given a Credit Card number, determine if it is Valid

'Here is a table outlining the major credit cards that you might want to validate.

'CARD TYPE  Prefix  Length  Check digit algorithm
'------------------------------------------------
'MASTERCARD 51-55   16      mod 10
'VISA       4       13, 16  mod 10
'AMEX       34
'           37      15      mod 10
'Diners Club/
'Carte Blanche
'           300-305
'           36
'           38      14      mod 10
'Discover   6011    16      mod 10
'enRoute    2014
'           2149    15      any
'JCB        3       16      mod 10
'JCB        2131
'           1800    15      mod 10


'2. LUHN Formula (Mod 10) for Validation of Primary Account Number
'The following steps are required to validate the primary account number:


'Step 1: Double the value of alternate digits of the primary account number beginning with the second digit from the right (the first right--hand digit is the check digit.)

'Step 2: Add the individual digits comprising the products obtained in Step 1 to each of the unaffected digits in the original number.

'Step 3: The total obtained in Step 2 must be a number ending in zero (30, 40, 50, etc.) for the account number to be validated.

'For example, to validate the primary account number 49927398716:

'Step 1:

'        4 9 9 2 7 3 9 8 7 1 6
'         x2  x2  x2  x2  x2
'------------------------------
'         18   4   6  16   2


'Step 2: 4 +(1+8)+ 9 + (4) + 7 + (6) + 9 +(1+6) + 7 + (2) + 6

'Step 3: Sum = 70: Card Number Is validated

'Note: Card is valid because the 70/10 yields no remainder.


Dim Tmp As Integer
Dim Counter As Integer
Dim CheckSum As Integer
Dim A_Nbr As String

A_Nbr = Trim(CCNumber)
CheckSum = 0
Counter = 0

' If card number has odd numner of digits,
' pad it with a leading zero.

If (Len(Trim(CCNumber)) Mod 2) = 1 Then
    A_Nbr = "0" & A_Nbr
End If


' If the product is greater than 10,
' add the two digits (14 = 1+4 = 5)
' Then sum these weighted digits.

For Counter = 1 To Len(A_Nbr)
    Tmp = CInt(MID(A_Nbr, Counter, 1))
    If (Counter Mod 2) = 1 Then             ' If Odd number
        Tmp = Tmp * 2
    End If
    If Tmp >= 10 Then
        Tmp = (Tmp Mod 10) + 1
    End If
    CheckSum = CheckSum + Tmp
Next

If (CheckSum Mod 10) = 0 Then
    ValidCC = True
Else
    ValidCC = False
End If

End Function

Download this snippet    Add to My Saved Code

Validate a credit card number. Accepts a credit card number and returns True if the Card Number is Comments

No comments have been posted about Validate a credit card number. Accepts a credit card number and returns True if the Card Number is . Why not be the first to post a comment about Validate a credit card number. Accepts a credit card number and returns True if the Card Number is .

Post your comment

Subject:
Message:
0/1000 characters