ProblemYou are writing an application that includes credit card processing and verification functionality. While the third-party credit card host will let you know when you have passed an invalid card number, you would like to catch invalid card numbers immediately when users enter them. SolutionSample code folder: Chapter 14\LuhnAlgorithm Use the Luhn Algorithm to determine if a credit card number is valid or not. The Luhn Algorithm (or Luhn Formula) was invented by Hans Peter Luhn of IBM in the 1960s as a method of verifying account numbers of varying lengths. It is also called a "modulus 10" formula because it uses the modulus 10 formula (x Mod 10 in Visual Basic) to confirm the number. DiscussionCreate a new Windows Forms application, and add the following controls to Form1:
Now add the following source code to the form's code template: Private Sub ActVerify_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ActVerify.Click ' ----- Check for a valid credit card number. Dim useCard As String = "" Dim oneDigit As String Dim counter As Integer ' ----- Create a string with just the digits of the card, ' just in case the user entered spaces or dashes ' between digit blocks. For counter = 1 To Len(CreditCard.Text) oneDigit = Mid(CreditCard.Text, counter, 1) If (IsNumeric(oneDigit) = True) Then _ useCard &= oneDigit Next counter If (useCard.Length = 0) Then MsgBox("Invalid card number.") ElseIf (VerifyCreditCard(useCard) = False) Then MsgBox("Invalid card number.") Else MsgBox("Card verified.") End If End Sub Private Function VerifyCreditCard(ByVal cardNumber _ As String) As Boolean ' ----- Given a card number, make sure it is valid. ' This method uses the Luhn algorithm to verify ' the number. This routine assumes that cardNumber ' contains only digits. Dim counter As Integer Dim digitTotal As Integer Dim holdValue As Integer Dim checkDigit As Integer Dim calcDigit As Integer Dim useCard As String ' ----- Perform some initial checks. useCard = Trim(cardNumber) If (IsNumeric(useCard) = False) Then Return False ' ----- Separate out the last digit, the check digit. ' For cards with an odd number of digits, ' prepend with a zero. If ((Len(useCard) Mod 2) <> 0) Then _ useCard = "0" & useCard checkDigit = useCard.Substring(Len(useCard) - 1, 1) useCard = useCard.Substring(0, Len(useCard) - 1) ' ----- Process each digit. digitTotal = 0 For counter = 1 To Len(useCard) If ((counter Mod 2) = 1) Then ' ----- This is an odd digit position. ' Double the number. holdValue = CInt(Mid(useCard, counter, 1)) * 2 If (holdValue > 9) Then ' ----- Process digits (16 becomes 1+6). digitTotal += (holdValue \ 10) + _ (holdValue - 10) Else digitTotal += holdValue End If Else ' ----- This is an even digit position. ' Simply add it. digitTotal += CInt(Mid(useCard, counter, 1)) End If Next counter ' ----- Calculate the 10's complement of both values. calcDigit = 10 - (digitTotal Mod 10) If (calcDigit = 10) Then calcDigit = 0 If (checkDigit = calcDigit) Then Return True Else _ Return False End Function Run the program, enter a credit card number, and click the Verify button to see if the card number is valid. |