Hello,
I am validating a credit card with the check can be described as follows.
For the number 49927389716
Reverse the digits: 61798372994
Sum the digits as odd positions: 6 + 7 + 9 + 7 + 9 + 4 = 42 = s1
The even positioned digits: 1, 8, 3, 2, 9 Multiply each even positioned digit by 2: 2, 16, 6, 4, 18
Sum the digits of each multiplication: 2, 7, 6, 4, 9 And them sum them: 2 + 7 + 6 + 4 + 9 = 28 = s2
Add s1 and s2 and perform mod 10 s1 + s2 = 70
which ends in zero which means that 49927398716 passes the Luhn test

Private Function ValidateLuhn(ByVal value As String) As Boolean0203 Dim CheckSum As Integer = 004 Dim DoubleFlag As Boolean = (value.Length Mod 2 = 0)0506 Dim Digit As Char07 Dim DigitValue As Integer08 For Each Digit In value09 DigitValue = Integer.Parse(Digit)10 If DoubleFlag Then11 DigitValue *= 212 If DigitValue > 9 Then13 DigitValue -= 914 End If15 End If16 CheckSum += DigitValue17 DoubleFlag = Not DoubleFlag18 Next1920 Return (CheckSum Mod 10 = 0)2122 End Function

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Public Class Form1 Private Sub OnMaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected MessageBox.Show(String.Format("{0} was rejected; {1}", e.Position, e.RejectionHint)) End Sub Private Sub OnTypeValidationCompleted(sender As Object, e As TypeValidationEventArgs) Handles MaskedTextBox1.TypeValidationCompleted If e.IsValidInput Then If e.ReturnValue.ToString().Length < 16 Then MessageBox.Show("The entered card number must contain 16 digits.") e.Cancel = True ElseIf Not e.ReturnValue.ToString().LuhnValidation() Then MessageBox.Show("Entered card number is invalid. Failed Luhn.") e.Cancel = True End If Else MessageBox.Show("The entered card number must contain 16 digits.") e.Cancel = True End If End SubEnd ClassModule Extensions <System.Runtime.CompilerServices.Extension()> _ Public Function LuhnValidation(ByVal source As String) As Boolean Dim result As Boolean = False Try ' We should validate that we are dealing with integers. If source.All(Function(c) Char.IsNumber(c)) Then ' Rules of algorithm ' 1. Reverse the order of the digits in the number. ' 2. Take the first, third, ... and every other odd digit in the reversed digits and sum them to form the partial sum s1 ' 3. Taking the second, fourth ... and every other even digit in the reversed digits: ' a. Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits ' b. Sum the partial sums of the even digits to form s2 ' 4. If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test. ' Code from - http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Visual_Basic_.NET result = source.Select(Function(c, i) (AscW(c) - 48) << ((source.Length - i - 1) And 1)).Sum(Function(n) If(n > 9, n - 9, n)) Mod 10 = 0 End If Catch ex As Exception result = False End Try Return result End FunctionEnd Module

Hello Saige,
Thanks for the advise but I get tese errors:
Error 1 'LuhnValidation' is not a member of 'String'
Error 2 'All' is not a member of 'String'.
Error 3 Expression expected.
Error 4 'Select' is not a member of 'String'.
Error 5 Expression expected
Error 6 Name 'i' is not declared.
Error 7 Name 'c' is not declared.
Error 8 Name 'i' is not declared.
Error 9 End of statement expected.

0

RIASAuthor Commented:

Public Function LuhnValidation(ByVal source As String) As Boolean Dim result As Boolean = False Try ' We should validate that we are dealing with integers. If source.All(Function(c) Char.IsNumber(c)) Then ' Rules of algorithm ' 1. Reverse the order of the digits in the number. ' 2. Take the first, third, ... and every other odd digit in the reversed digits and sum them to form the partial sum s1 ' 3. Taking the second, fourth ... and every other even digit in the reversed digits: ' a. Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits ' b. Sum the partial sums of the even digits to form s2 ' 4. If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test. ' Code from - http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers#Visual_Basic_.NET result = source.Select(Function(c, i) (AscW(c) - 48) << ((source.Length - i - 1) And 1)).Sum(Function(n) If(n > 9, n - 9, n)) Mod 10 = 0 End If Catch ex As Exception result = False End Try Return result End Function

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Private Function ValidateLuhn(cardnumber As String) As Boolean Dim _charArray As Char() = cardnumber.ToCharArray() Array.Reverse(_charArray) Dim _odds As Integer = 0, _evens As Integer = 0, _num As Integer, _tmp As Integer Dim _valid As Boolean = False For _index As Integer = 0 To _charArray.Length() - 1 _num = Integer.Parse(_charArray(_index)) If (_index + 1) Mod 2 = 0 Then _tmp = _num * 2 If _tmp > 10 Then _evens += Integer.Parse(_tmp.ToString().Substring(0, 1)) + Integer.Parse(_tmp.ToString().Substring(1, 1)) Else _evens += _tmp End If Else _odds += _num End If Next _num = _odds + _evens Return ((_num Mod 10) = 0)End Function

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Private Function LuhnValidation(ByVal source As String) As Boolean Dim result As Boolean = False Try ' We should validate that we are dealing with integers. If Not IsNumeric(source) Then Exit Function ' Rules of algorithm ' 1. Reverse the order of the digits in the number. ' 2. Take the first, third, ... and every other odd digit in the reversed digits and sum them to form the partial sum s1 ' 3. Taking the second, fourth ... and every other even digit in the reversed digits: ' a. Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits ' b. Sum the partial sums of the even digits to form s2 ' 4. If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test. Dim sum As Integer = 0 For i As Integer = source.Length - 1 To 0 Step -1 If i Mod 2 <> 0 Then sum = sum + (AscW(source(i)) - 48) Else If ((AscW(source(i)) - 48) * 2) > 9 Then sum = sum + ((((AscW(source(i)) - 48) * 2) \ 10) + (((AscW(source(i)) - 48) * 2) - 10)) Else sum = sum + ((AscW(source(i)) - 48) * 2) End If End If Next result = sum Mod 10 = 0 Catch ex As Exception result = False End Try Return resultEnd Function

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for freeEdge Out The Competitionfor your dream job with proven skills and certifications.Get started todayStand Outas the employee with proven skills.Start learning today for freeMove Your Career Forwardwith certification training in the latest technologies.Start your trial today

Open in new window

-saige-