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
#2
Well, you don't actually need to create a new module, you simply need to pop this into your code:

[code2=vbnet]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 ValidLuhn As Boolean = False
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
Return ValidLuhn
End Function[/code2]

Then, when the textbox loses focus, just call:
[code2=vbnet]If (ValidLuhn(TextBox1.Text)) Then
MsgBox("Valid!")
End If[/code2]

And there ya go!
My Blog | My Setup | My Videos | Have a wonderful day.
#3
Thanks Brandon,

Thanks for your response. While waiting, I tried the following code and it seems to work very well:

Code:
Dim checksum As Integer = 0
        Dim doubleflag As Boolean = (value.Length Mod 2 = 0)

        Dim digit As Char
        Dim digitvalue As Integer
        For Each digit In value
            digitvalue = Integer.Parse(digit)
            If doubleflag Then
                digitvalue *= 2
                If digitvalue > 9 Then
                    digitvalue -= 9
                End If
            End If
            checksum += digitvalue
            doubleflag = Not doubleflag
        Next
        Return (checksum Mod 10 = 0)
    End Function

I will give try adding the Return line as suggested, and see how well this works.
//Kismet
#4
Brandon,

I noticed that the first declared variable - ValidLuhn - has the same name as the function name. Won't this pose a problem?
//Kismet
#5
Ah yes, you are correct! Apparently, when you create a function, you actually create a variable with the function's name! I was unaware of that!

So yes, you are going to want to replace:
[code2=vbnet]Dim ValidLuhn As Boolean = False[/code2]

With:
[code2=vbnet]ValidLuhn = False[/code2]
My Blog | My Setup | My Videos | Have a wonderful day.


Forum Jump:


Users browsing this thread: 1 Guest(s)