TextBox Validation

Checking if credit card is valid

Page 1 of 1

6 Replies - 2299 Views - Last Post: 19 November 2009 - 11:25 AM Rate Topic: -----

#1 fierce vbhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-November 09

TextBox Validation

Post icon  Posted 18 November 2009 - 03:47 AM

Hello guys am new to the forum could anyone help me with this.
I am trying to mkae sure that there has been 16 numeric digits entered into the masked textbox. If not show errror message and if so to call the fucnction ValidateLuhn. When calling Validate Luhn i want the program to tell me if the number entered is valid or invalid using the code in the function:

	Private Function ValidateLuhn(ByVal value As String) As Boolean

		Dim CheckSum As Integer = 0
		Dim DoubleFlag As Boolean = (value.Length Mod 2 = 0)

		Dim Digit As Char
		Dim DigitValue As Integer
		For Each Digit In value
			DigitValue = Integer.Parse(Digit)
			If DoubleFlag Then
				DigitValue *= 2
				If DigitValue > 9 Then
					DigitValue -= 9
				End If
			End If
			CheckSum += DigitValue
			DoubleFlag = Not DoubleFlag
		Next

		Return (CheckSum Mod 10 = 0)

	End Function

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		Const numberlength As Integer = 16
		If MaskedTextBox1.Text.Length < numberlength Then
			MessageBox.Show("Not enough characters entered")
		ElseIf IsNumeric(MaskedTextBox1.Text) = True Then
			Dim success As Boolean = ValidateLuhn(MaskedTextBox1.Text)


			If success = False Then
				MessageBox.Show("Invalid")
			End If
		End If



	End Sub



Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: TextBox Validation

#2 theunborncoder  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 123
  • Joined: 15-October 09

Re: TextBox Validation

Posted 18 November 2009 - 04:39 AM

Hi

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 Dim success As Boolean=false 

		Const numberlength As Integer = 16
		If MaskedTextBox1.Text.Length < numberlength Then
			MessageBox.Show("Not enough characters entered")
	   else
		   If IsNumeric(MaskedTextBox1.Text) = True Then
			
				success = ValidateLuhn(MaskedTextBox1.Text)


			If success = False Then
				MessageBox.Show("Invalid")
					   else
						 MessageBox.Show("valid")
			End If
		End If
		 End If
	End Sub


This post has been edited by theunborncoder: 18 November 2009 - 04:42 AM

Was This Post Helpful? 1
  • +
  • -

#3 fierce vbhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-November 09

Re: TextBox Validation

Posted 18 November 2009 - 06:42 AM

Hi im still having difficulty with this. It is telling me when there are not enough characters entered however when there has been i want the function ValidateLuhn to run. Using the function a number such as "5191701142626689" should produce a valid result whilst a number suchas 1111111111111111 will not. Is there any way to solve this?
Was This Post Helpful? 0
  • +
  • -

#4 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: TextBox Validation

Posted 18 November 2009 - 06:52 AM

well that depend on the how the numbers is generated. i mean there is some formula that make this numbers they are not random i think. it is like barcode there is a formula of making them.

This post has been edited by NoBrain: 18 November 2009 - 06:53 AM

Was This Post Helpful? 0
  • +
  • -

#5 fierce vbhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-November 09

Re: TextBox Validation

Posted 18 November 2009 - 08:32 AM

The numbers are credit card numbers.They are manually typed into the masked textbox on the form. As long as they are 16digits long then the ValidateLuhn function should run. This function should determine whether the number entered is a valiid credit card number or not.
Was This Post Helpful? 0
  • +
  • -

#6 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: TextBox Validation

Posted 18 November 2009 - 08:47 AM

I don't see any problem - the code works - just need to add the extra "is valid" messagebox as unborncoder showed.

The only other thing I can think of is - do you have the custom mask set up correctly for the maskedinput field? (16 zeroes)
Was This Post Helpful? 0
  • +
  • -

#7 fierce vbhead  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 18-November 09

Re: TextBox Validation

Posted 19 November 2009 - 11:25 AM

I now have the program working smoothly. Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1