Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 132,214 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 956 people online right now. Registration is fast and FREE... Join Now!




Check digit on credit card last four numbers

 
Reply to this topicStart new topic

Check digit on credit card last four numbers

difficultproblems
post 20 Feb, 2008 - 01:33 PM
Post #1


New D.I.C Head

*
Joined: 19 Feb, 2008
Posts: 4

I can't seem to get the right solution. Can anyone add or give me some advice on this solution.


CODE
Dim creditNum As String
        Dim checkDigit As String
        Dim sumOfDigits As Integer
        Dim num1 As Integer
        Dim num2 As Integer
        Dim num3 As Integer
        Dim num4 As Integer

        creditNum = Me.xNumberTextBox.Text

        ' verify that the credit card number contains five digits
        If creditNum Like "#####" Then
            ' calculate correct check digit
            Integer.TryParse(creditNum.Substring(0, 1), num1)
            Integer.TryParse(creditNum.Substring(1, 1), num2)
            Integer.TryParse(creditNum.Substring(2, 1), num3)
            Integer.TryParse(creditNum.Substring(3, 1), num4)

            'calculate sum of digit

            sumOfDigits = num1 + num2 + num3 + num4

            'calculate check digit
            creditNum = "#####"
            num1 = num1
            num2 = num1 * 2
            num3 = num2
            num4 = num3 * 2
            checkDigit = CStr(sumOfDigits)


            ' determine whether the credit card number ends with the correct check digit
            If creditNum.EndsWith(checkDigit) Then
                ' processed when the credit card is valid
                Me.xMessageLabel.Text = "The credit card number is valid."
            Else
                ' processed when the credit card is not valid
                Me.xMessageLabel.Text = "The credit card number is not valid."
            End If
        Else    ' processed when the user does not enter five digits
            MessageBox.Show("Please enter a five-digit number.", "Georgetown Credit", _
                MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        Me.xNumberTextBox.Focus()
    End Sub
End Class
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 20 Feb, 2008 - 03:19 PM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


First use code.gif tags when posting code. I have moved this to the VB.NET forum.

Second you are going to need to describe in as much detail what you are having trouble with. Just posting your code and saying I can't get it is not going to provide us with any insight as to what is wrong.
User is offlineProfile CardPM

Go to the top of the page

difficultproblems
post 20 Feb, 2008 - 05:57 PM
Post #3


New D.I.C Head

*
Joined: 19 Feb, 2008
Posts: 4

QUOTE(difficultproblems @ 20 Feb, 2008 - 02:33 PM) *

I can't seem to get the right solution. Can anyone add or give me some advice on this solution.


I am having problems with the sum of digit and the check digit.


CODE
Dim creditNum As String
        Dim checkDigit As String
        Dim sumOfDigits As Integer
        Dim num1 As Integer
        Dim num2 As Integer
        Dim num3 As Integer
        Dim num4 As Integer

        creditNum = Me.xNumberTextBox.Text

        ' verify that the credit card number contains five digits
        If creditNum Like "#####" Then
            ' calculate correct check digit
            Integer.TryParse(creditNum.Substring(0, 1), num1)
            Integer.TryParse(creditNum.Substring(1, 1), num2)
            Integer.TryParse(creditNum.Substring(2, 1), num3)
            Integer.TryParse(creditNum.Substring(3, 1), num4)

            'calculate sum of digit

            sumOfDigits = num1 + num2 + num3 + num4

            'calculate check digit
            creditNum = "#####"
            num1 = num1
            num2 = num1 * 2
            num3 = num2
            num4 = num3 * 2
            checkDigit = CStr(sumOfDigits)


            ' determine whether the credit card number ends with the correct check digit
            If creditNum.EndsWith(checkDigit) Then
                ' processed when the credit card is valid
                Me.xMessageLabel.Text = "The credit card number is valid."
            Else
                ' processed when the credit card is not valid
                Me.xMessageLabel.Text = "The credit card number is not valid."
            End If
        Else    ' processed when the user does not enter five digits
            MessageBox.Show("Please enter a five-digit number.", "Georgetown Credit", _
                MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        Me.xNumberTextBox.Focus()
    End Sub
End Class


User is offlineProfile CardPM

Go to the top of the page

Jayman
post 20 Feb, 2008 - 07:51 PM
Post #4


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


As I originally stated, describe the problem in detail.

What are the problems you are having?
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 08:24PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month