VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

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




5-digit credit card number with fifth digit as check digit

 

5-digit credit card number with fifth digit as check digit, when I run the application I can't get a "valid" message

tenec

31 Mar, 2009 - 01:58 PM
Post #1

New D.I.C Head
*

Joined: 22 Feb, 2009
Posts: 25

I just finished this application where you enter a five-digit credit card number, with the fifth digit being the check digit. everything seems to work except, I can't seem to get it to determine that any number is "valid". Every 5-digit combination that I enter returns "The credit card number is not valid". Does any one have any suggestions? Could someone help. I'm embarrased and stuck.

CODE

    Private Sub xNumberTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.Enter
        Me.xNumberTextBox.SelectAll()
    End Sub

    Private Sub xNumberTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.TextChanged
        Me.xMessageLabel.Text = String.Empty
    End Sub

    Private Sub xVerifyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xVerifyButton.Click
        ' get a 5-digit number from the user

        Dim cardNum 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

        cardNum = Me.xNumberTextBox.Text

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

            'calculate sum of digit

            sumOfDigits = num1 + num2 + num3 + num4

            'calculate check digit
            cardNum = "#####"
            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 cardNum.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
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 5)

June7

RE: 5-digit Credit Card Number With Fifth Digit As Check Digit

8 Apr, 2009 - 04:37 PM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Dec, 2008
Posts: 486



Thanked: 38 times
My Contributions
Maybe this will have result you intended with the wild cards:
CODE
If String.Length() >= 5 and String.IsNumeric() Then


This post has been edited by June7: 8 Apr, 2009 - 04:43 PM
User is offlineProfile CardPM
+Quote Post

egof

RE: 5-digit Credit Card Number With Fifth Digit As Check Digit

9 Apr, 2009 - 04:11 AM
Post #3

D.I.C Head
Group Icon

Joined: 27 Mar, 2009
Posts: 96



Thanked: 12 times
Dream Kudos: 100
My Contributions
What numbers are you using? Using a single character for the checksum reduces the numbers you can use for the first 4 characters. Your first 4 characters would be mostly #s less than 4
User is offlineProfile CardPM
+Quote Post

dbasnett

RE: 5-digit Credit Card Number With Fifth Digit As Check Digit

9 Apr, 2009 - 05:52 AM
Post #4

D.I.C Head
**

Joined: 1 Oct, 2008
Posts: 224



Thanked: 14 times
My Contributions
CODE
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'test
        Dim chknum As String = "9871"
        'create a check digit
        chknum &= genChkDig(chknum).ToString
        'check the check digit
        If chknum.Length = 5 AndAlso chknum.Substring(4, 1) = genChkDig(chknum.Substring(0, 4)) Then
            Stop 'check digit correct
        Else
            Stop 'error
        End If
        chknum = "3216"
        'create a check digit
        chknum &= genChkDig(chknum).ToString
        'check the check digit
        If chknum.Length = 5 AndAlso chknum.Substring(4, 1) = genChkDig(chknum.Substring(0, 4)) Then
            Stop 'check digit correct
        Else
            Stop 'error
        End If
    End Sub
    Private Function genChkDig(ByVal s As String) As Integer
        If s.Length <> 4 Then Return -1 'error
        Dim sumem As Integer = 0
        Dim i As Integer
        For x As Integer = 0 To s.Length - 1
            If Integer.TryParse(s.Substring(x, 1), i) Then
                sumem += i
            Else
                Return -1 'error
            End If
        Next
        If sumem Mod 10 = 0 Then Return 1 Else Return sumem Mod 10
    End Function

User is offlineProfile CardPM
+Quote Post

tenec

RE: 5-digit Credit Card Number With Fifth Digit As Check Digit

11 Apr, 2009 - 12:03 PM
Post #5

New D.I.C Head
*

Joined: 22 Feb, 2009
Posts: 25

QUOTE(dbasnett @ 9 Apr, 2009 - 05:52 AM) *

CODE
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'test
        Dim chknum As String = "9871"
        'create a check digit
        chknum &= genChkDig(chknum).ToString
        'check the check digit
        If chknum.Length = 5 AndAlso chknum.Substring(4, 1) = genChkDig(chknum.Substring(0, 4)) Then
            Stop 'check digit correct
        Else
            Stop 'error
        End If
        chknum = "3216"
        'create a check digit
        chknum &= genChkDig(chknum).ToString
        'check the check digit
        If chknum.Length = 5 AndAlso chknum.Substring(4, 1) = genChkDig(chknum.Substring(0, 4)) Then
            Stop 'check digit correct
        Else
            Stop 'error
        End If
    End Sub
    Private Function genChkDig(ByVal s As String) As Integer
        If s.Length <> 4 Then Return -1 'error
        Dim sumem As Integer = 0
        Dim i As Integer
        For x As Integer = 0 To s.Length - 1
            If Integer.TryParse(s.Substring(x, 1), i) Then
                sumem += i
            Else
                Return -1 'error
            End If
        Next
        If sumem Mod 10 = 0 Then Return 1 Else Return sumem Mod 10
    End Function



User is offlineProfile CardPM
+Quote Post

tenec

RE: 5-digit Credit Card Number With Fifth Digit As Check Digit

11 Apr, 2009 - 12:18 PM
Post #6

New D.I.C Head
*

Joined: 22 Feb, 2009
Posts: 25

I actually got it to work using this code but you were very helpful in getting me to focus on where the issue was. I was not realizing that using a single character for the checksum reduces the numbers you can use for the first 4 characters. Your first 4 characters would be mostly #s less than 4.

Thanks every one , you were very helpful. I hope that I can be equally as helpful soon.

CODE
Option Explicit On
Option Strict On

Public Class MainForm

    Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
        Me.Close()
    End Sub

    Private Sub xNumberTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.Enter
        Me.xNumberTextBox.SelectAll()
    End Sub

    Private Sub xNumberTextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xNumberTextBox.TextChanged
        Me.xMessageLabel.Text = String.Empty
    End Sub

    Private Sub xVerifyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xVerifyButton.Click
        ' get a 5-digit number from the user

        Dim cardNum 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
        Dim num5 As Integer

        cardNum = Me.xNumberTextBox.Text

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


            'calculate sum of digit

            sumOfDigits = num1 + num2 + num3 + num4


            ' determine whether the credit card number ends with the correct check digit
            If num5 = sumOfDigits 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
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/24/09 01:47PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month