Help with Credit Card Validation - Printable Version +- BP Forums (https://bpforums.info) +-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55) +--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56) +---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8) +----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9) +----- Thread: Help with Credit Card Validation (/showthread.php?tid=613) |
Help with Credit Card Validation - kismetgerald - 08-24-2012 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-validation-code-vb-net-generic/ Code: Module LuhnValidate 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. Re: Help with Credit Card Validation - brandonio21 - 08-24-2012 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! Re: Help with Credit Card Validation - kismetgerald - 08-25-2012 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 I will give try adding the Return line as suggested, and see how well this works. Re: Help with Credit Card Validation - kismetgerald - 08-25-2012 Brandon, I noticed that the first declared variable - ValidLuhn - has the same name as the function name. Won't this pose a problem? Re: Help with Credit Card Validation - brandonio21 - 08-25-2012 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] |