Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Credit Card Validation
#1
Hey guys,

It's me again - still learning vb.net actively and getting lots of help from communities such as this - thanks!

So, I came across the following code that can be used in a vb.net application to validate a credit card number. How would I use this in my application? I was thinking of creating a module, and then calling the module in the LostFocus event of the TextBox holding the string. However, what I couldn't figure out was how to pass the string to the code in the module. In other words, how does the module know to validate the credit card number entered in the TextBox?

Here's the code.
SOURCE: http://www.robin-janssens.com/misc/luhn-...t-generic/
Code:
Module LuhnValidate

    Private Function ValidLuhn(ByVal Luhn As String) As Boolean
        'this function accepts any 16 digit Luhn as long as it is numeric but checks 15 digit ones with the luhn algorithm
        Dim ThisDigit As String
        Dim Length As Integer
        Dim N As Integer
        Dim RunningTotal As Integer

        If IsNumeric(Luhn) Then
            'reverse the Luhn number and find out it's length
            Luhn = StrReverse(Luhn)
            Length = Len(Luhn)

            'process each individual digit
            For N = 1 To Length
                ThisDigit = Luhn.Substring(N - 1, 1)

                'if this number is the 2nd, 4th, 6th etc digit apply some extra processing
                If N Mod 2 = 0 Then
                    'double the digit
                    ThisDigit *= 2
                    'if the digit is greater than 9 further processing is needed
                    If ThisDigit > 9 Then
                        'add both individual digits up and add them to the running total
                        ThisDigit = Val(ThisDigit.Substring(0, 1)) + Val(ThisDigit.Substring(1, 1))
                        RunningTotal += ThisDigit
                    Else
                        'otherwise just add the number to the running total
                        RunningTotal += ThisDigit
                    End If
                Else
                    'otherwise just add this digit to the running total
                    RunningTotal += ThisDigit
                End If
            Next N

            'if the resulting number is divisible by 10, the Luhn is correct
            If RunningTotal Mod 10 = 0 Then
                ValidLuhn = True
            End If
        End If
    End Function

End Module

Also, you should know that when I entered this code in Visual Basic - intellisense underlined the last line "End Module". When I hover over it, this is what is says:

Quote:Function 'ValidLuhn' doesn't return a value on all code paths. Are you missing a 'Return' statement?

Your help would be really appreciated, thanks.
//Kismet


Messages In This Thread
Help with Credit Card Validation - by kismetgerald - 08-24-2012, 09:24 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)