Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar MSDN Visual Basic Community

Title Validate a credit card number credit card, Visa, Master Card, American Express, Discover, EnRoute, JCB, Diners Club, Carte Blanche Algorithms, Miscellany, Strings

This example shows how to validate a credit card number to protect against typos.

The program first verifies that the number has 16 digits. It then checks the digits starting from the end. If a digit's position in the number is odd (numbered starting with digit 1 at the end), the program adds its value to a checksum value. If the digit's position is even, the program doubles it and adds the resulting value's digit(s) to the checksum value.

For example, suppose the number is 1234567890123456. The checksum value would be:

```    6 + [2*5] + 4 + [2*3] + 2 + [2*1] + 0 + [2*9] +
8 + [2*7] + 6 + [2*5] + 4 + [2*3] + 2 + [2*1] =
6 + [10] + 4 + [6] + 2 + [2] + 0 + [18] +
8 + [14] + 6 + [10] + 4 + [6] + 2 + [2] =
6 + 1+0 + 4 + 6 + 2 + 2 + 0 + 1+8 +
8 + 1+4 + 6 + 1+0 + 4 + 6 + 2 + 2 =
64```

If the card number is valid, the result will be a multiple of 10. This example gives 64 which is not a multiple of 10 so this is an invalid number.

Different types of credit cards have different prefixes. For example, Visa cards all begin with the digit 4. After the program verifies that the number has a correct checksum, it checks that the user entered the right card type for the number's prefix.

Note that this test helps protect against a user incorrectly entering a credit card number. It doesn't prevent someone from cooking up a non-existent number that has a valid checksum and prefix. That's pretty easy to do.

```' Return True if the credit card number
' makes sense.
Public Function ValidCard(ByVal card_number As String, _
ByVal card_type As String) As Boolean
Dim num_digits As Integer
Dim ch As String
Dim i As Integer
Dim odd_digit As Boolean
Dim doubled_digit As String
Dim checksum As Integer

' Assume the validation will fail.
ValidCard = False

' Make sure we have a card type.
If Len(card_type) = 0 Then Exit Function

' Remove any spaces.
card_number = Replace(card_number, " ", "")

' Make sure we have 16 digits.
If Len(card_number) <> 16 Then Exit Function

' Examine the digits.
odd_digit = False
For i = Len(card_number) To 1 Step -1
ch = Mid\$(card_number, i, 1)
If ch < "0" Or ch > "9" Then
' Not a digit. Validation fails.
Exit Function
Else
' Process this digit.
odd_digit = Not odd_digit
If odd_digit Then
' Odd digit. Add it to the checksum
checksum = checksum + CInt(ch)
Else
' Even digit. Double it and add the
' digits in the result to the checksum
doubled_digit = Format\$(2 * CInt(ch))
checksum = checksum + _
CInt(Left\$(doubled_digit, 1))
If Len(doubled_digit) = 2 Then checksum = _
checksum + CInt(Mid\$(doubled_digit, 2, _
1))
End If
End If
Next i

' Check the checksum.
If (checksum Mod 10) <> 0 Then Exit Function

' Check the card type.
ValidCard = (card_type = CardType(card_number))
End Function```