School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,138 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,864 people online right now. Registration is fast and FREE... Join Now!



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

Page 1 of 1

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

#1 tenec  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 25
  • Joined: 22-February 09


Dream Kudos: 0

Posted 31 March 2009 - 01:58 PM

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.

	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

Was This Post Helpful? 0
  • +
  • -


#2 June7  Icon User is offline

  • D.I.C Addict
  • Icon
  • Group: Members w/DIC++
  • Posts: 591
  • Joined: 09-December 08


Dream Kudos: 0

Posted 08 April 2009 - 04:37 PM

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

This post has been edited by June7: 08 April 2009 - 04:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 egof  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Members w/DIC++
  • Posts: 98
  • Joined: 27-March 09


Dream Kudos: 100

Posted 09 April 2009 - 04:11 AM

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
Was This Post Helpful? 0
  • +
  • -

#4 dbasnett  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 243
  • Joined: 01-October 08


Dream Kudos: 0

Posted 09 April 2009 - 05:52 AM

	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
 

Was This Post Helpful? 0
  • +
  • -

#5 tenec  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 25
  • Joined: 22-February 09


Dream Kudos: 0

Posted 11 April 2009 - 12:03 PM

View Postdbasnett, on 9 Apr, 2009 - 05:52 AM, said:

	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
 

Was This Post Helpful? 0
  • +
  • -

#6 tenec  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 25
  • Joined: 22-February 09


Dream Kudos: 0

Posted 11 April 2009 - 12:18 PM

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.

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


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month