Numeric Validation

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 16883 Views - Last Post: 26 January 2007 - 05:10 PM Rate Topic: -----

#1 INeedHelpPlease  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-January 07

Numeric Validation

Posted 25 January 2007 - 04:02 PM

Public Class FrmDrivingTest3

	Private Sub FrmDrivingTest3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	End Sub

	Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

		Dim StartValue As Integer			   'Declares Variable StartValue As Integer
		Dim MaximumValue As Integer			 'Declares Variable MaximumValue As Integer
		Dim MultiplierValue As Integer		  'Declares Variable MultiplierValue As Integer
		Dim Answer As String					'Declares Variable Answer As String
		Dim Concatenation As Integer			'Declares Variable Concatenation As Integer
		Dim Hold As Integer					 'Declares Variable Hold As Integer

		If IsNumeric(txtStartValue.Text) = True Then	'Checks if txtstartvalue is numeric

			StartValue = txtStartValue.Text
		Else
			txtStartValue.Clear()
			MessageBox.Show("Startvalue: Operation not valid") 'Messagebox Will Appear If Not Valid

		End If

		If IsNumeric(txtMaximumValue.Text) = True Then  'Checks if txtmaximumvalue is numeric

			MaximumValue = txtMaximumValue.Text
		Else
			txtMaximumValue.Clear()
			MessageBox.Show("Maximum Value: Operation not valid") 'Messagebox Will Appear If Not Valid

		End If

		If IsNumeric(txtMultiplierValue.Text) = True Then   'Checks if txtmultipliervalue is numeric

			MultiplierValue = txtMultiplierValue.Text
		Else
			txtMultiplierValue.Clear()
			MessageBox.Show("Multiplier Value: Operation not valid") 'Messagebox Will Appear If Not Valid

		End If


		If Me.txtStartValue.Text < 1 Then
			MessageBox.Show("Start Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Else
			StartValue = txtStartValue.Text
		End If

		If txtMaximumValue.Text < 1 Then
			MessageBox.Show("Maximum Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Else
			MaximumValue = Me.txtMaximumValue.Text

		End If

		If StartValue > Me.txtMaximumValue.Text Then
			MessageBox.Show("Start Value Must Be Less Than Maximum Value") 'If Start Value Is Higher Than Maximum Value Error Message Will Appear
		Else
			MaximumValue = Me.txtMaximumValue.Text

		End If

		Do While StartValue <= MaximumValue 'This Is The Loops To Make The Program Work
			Answer = StartValue
			Hold = StartValue
			Do While Concatenation < 5
				Answer = Answer & "" & Hold
				Concatenation += 1
			Loop
			Concatenation = 0
			Hold = 0
			Me.lstResults.Items.Add(Answer) 'Answers Will Appear
			Answer = " "
			StartValue = StartValue * MultiplierValue
		Loop

	End Sub
End Class


Hi need help.the numeric checking part is not working and also entering 0 isnt working either. can someone help?

Is This A Good Question/Topic? 0
  • +

Replies To: Numeric Validation

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Numeric Validation

Posted 25 January 2007 - 04:49 PM

Anyplace in your code where you are taking a value from a textbox and storing it into a numeric variable data type it must be converted to the proper data type. When you access a textbox.text property the value returned is a String.

Needs be like this:
StartValue = CInt(txtStartValue.Text)


Or this:
StartValue = Convert.ToInt32(txtStartValue.Text)



You have several lines of code that this will need to be done.

Same problem with the test condition in this IF statement. You are trying to compare a String value to a number, it won't work.
If Me.txtStartValue.Text < 1 Then



Convert the value to numeric:
If CInt(Me.txtStartValue.Text) < 1 Then


Was This Post Helpful? 0
  • +
  • -

#3 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Numeric Validation

Posted 25 January 2007 - 04:54 PM

You can use numbers what are stored as literal words as numbers directly! Even using a double to integer would produce mistakes, so just use replace all the those and it sould do what you want:
StartValue = CType(val(txtStartValue.Text), Integer)
MaximumValue = CType(val(txtMaximumValue.Text), Integer)


Note that function val() would make make a numeric data stored as string in a numeric form, but ctype would make it a correct type for your declared types.

Hope it helped :D

--To late :P

This post has been edited by m2s87: 25 January 2007 - 04:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 INeedHelpPlease  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-January 07

Re: Numeric Validation

Posted 26 January 2007 - 03:13 AM

I did it all.but it then crashed when putting 0 in then when i entered letter it came up with "conversion from string "" to type 'integer' is not valid. Any chance anyone could change the code fully for me?
Was This Post Helpful? 0
  • +
  • -

#5 INeedHelpPlease  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-January 07

Re: Numeric Validation

Posted 26 January 2007 - 05:31 AM

Public Class FrmDrivingTest3

	Private Sub FrmDrivingTest3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	End Sub

	Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

		Dim StartValue As Integer			   'Declares Variable StartValue As Integer
		Dim MaximumValue As Integer			 'Declares Variable MaximumValue As Integer
		Dim MultiplierValue As Integer		  'Declares Variable MultiplierValue As Integer
		Dim Answer As String					'Declares Variable Answer As String
		Dim Concatenation As Integer			'Declares Variable Concatenation As Integer
		Dim Hold As Integer					 'Declares Variable Hold As Integer

		If IsNumeric(txtStartValue.Text) = True Then	'Checks if txtstartvalue is numeric

			StartValue = CInt(txtStartValue.Text)
		Else
			txtStartValue.Clear()
			MessageBox.Show("Startvalue: Operation not valid") 'Messagebox Will Appear If Not Valid

		End If

		If IsNumeric(txtMaximumValue.Text) = True Then  'Checks if txtmaximumvalue is numeric

			MaximumValue = CInt(txtMaximumValue.Text)
		Else
			txtMaximumValue.Clear()
			MessageBox.Show("Maximum Value: Operation not valid") 'Messagebox Will Appear If Not Valid

		End If

		If IsNumeric(txtMultiplierValue.Text) = True Then   'Checks if txtmultipliervalue is numeric

			StartValue = CInt(txtMultiplierValue.Text)
		Else
			txtMultiplierValue.Clear()
			MessageBox.Show("Multiplier Value: Operation not valid") 'Messagebox Will Appear If Not Valid

		End If


		If CInt(Me.txtStartValue.Text) < 1 Then
			MessageBox.Show("Start Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Else
			StartValue = txtStartValue.Text
		End If

		If CInt(Me.txtMaximumValue.Text) < 1 Then
			MessageBox.Show("Maximum Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Else
			MaximumValue = txtMaximumValue.Text

		End If

		If CInt(Me.txtMultiplierValue.Text) < 1 Then
			MessageBox.Show("Maximum Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Else
			MultiplierValue = txtMultiplierValue.Text

		End If

		If StartValue > Me.txtMaximumValue.Text Then
			MessageBox.Show("Start Value Must Be Less Than Maximum Value") 'If Start Value Is Higher Than Maximum Value Error Message Will Appear
		Else
			MaximumValue = CInt(txtMaximumValue.Text)

		End If

		Do While StartValue <= MaximumValue 'This Is The Loops To Make The Program Work
			Answer = StartValue
			Hold = StartValue
			Do While Concatenation < 5
				Answer = Answer & "" & Hold
				Concatenation += 1
			Loop
			Concatenation = 0
			Hold = 0
			Me.lstResults.Items.Add(Answer) 'Answers Will Appear
			Answer = " "
			StartValue = StartValue * MultiplierValue
		Loop

	End Sub
End Class



when i check to see if it isnumeric it checks it then the messages appear but then it crashes and comes up with this message.

conversion from string "" to type integer is not valid

then when i enter 0 all the right messages appear then it crashes

can anyone help me please
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Numeric Validation

Posted 26 January 2007 - 06:27 AM

Topics merged
Was This Post Helpful? 0
  • +
  • -

#7 INeedHelpPlease  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-January 07

Re: Numeric Validation

Posted 26 January 2007 - 06:47 AM

View PostAmadeus, on 26 Jan, 2007 - 06:27 AM, said:

Topics merged


how do i make it work then?
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Numeric Validation

Posted 26 January 2007 - 10:21 AM

You still are trying to store String values in numeric variables. Finishing correcting your code as I previously mentioned.

Fix these lines.
StartValue = txtStartValue.Text
MaximumValue = txtMaximumValue.Text
MultiplierValue = txtMultiplierValue.Text


Was This Post Helpful? 0
  • +
  • -

#9 INeedHelpPlease  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-January 07

Re: Numeric Validation

Posted 26 January 2007 - 11:35 AM

Public Class FrmDrivingTest3

	Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

		Dim StartValue As Integer			   'Declares Variable StartValue As Integer
		Dim MaximumValue As Integer			 'Declares Variable MaximumValue As Integer
		Dim MultiplierValue As Integer		  'Declares Variable MultiplierValue As Integer
		Dim Answer As String					'Declares Variable Answer As String
		Dim Concatenation As Integer			'Declares Variable Concatenation As Integer
		Dim Hold As Integer					 'Declares Variable Hold As Integer

		If IsNumeric(txtStartValue.Text) = True Then	'Checks if txtstartvalue is numeric

			StartValue = CInt(txtStartValue.Text)
		Else
			txtStartValue.Clear()
			MessageBox.Show("Startvalue: Operation not valid") 'Messagebox Will Appear If Not Valid

		End If

		If IsNumeric(txtMaximumValue.Text) = True Then  'Checks if txtmaximumvalue is numeric

			MaximumValue = CInt(txtMaximumValue.Text)
		Else
			txtMaximumValue.Clear()
			MessageBox.Show("Maximum Value: Operation not valid") 'Messagebox Will Appear If Not Valid

		End If

		If IsNumeric(txtMultiplierValue.Text) = True Then   'Checks if txtmultipliervalue is numeric

			StartValue = CInt(txtMultiplierValue.Text)
		Else
			txtMultiplierValue.Clear()
			MessageBox.Show("Multiplier Value: Operation not valid") 'Messagebox Will Appear If Not Valid

		End If


		If CInt(Me.txtStartValue.Text) < 1 Then
			MessageBox.Show("Start Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Else
			StartValue = CInt(txtStartValue.Text)
		End If

		If CInt(Me.txtMaximumValue.Text) < 1 Then
			MessageBox.Show("Maximum Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Else
			MaximumValue = CInt(txtMaximumValue.Text)

		End If

		If CInt(Me.txtMultiplierValue.Text) < 1 Then
			MessageBox.Show("Maximum Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Else
			MultiplierValue = CInt(txtMultiplierValue.Text)

		End If

		If StartValue > Me.txtMaximumValue.Text Then
			MessageBox.Show("Start Value Must Be Less Than Maximum Value") 'If Start Value Is Higher Than Maximum Value Error Message Will Appear
		Else
			MaximumValue = CInt(txtMaximumValue.Text)

		End If

		Do While StartValue <= MaximumValue 'This Is The Loops To Make The Program Work
			Answer = StartValue
			Hold = StartValue
			Do While Concatenation < 5
				Answer = Answer & "" & Hold
				Concatenation += 1
			Loop
			Concatenation = 0
			Hold = 0
			Me.lstResults.Items.Add(Answer) 'Answers Will Appear
			Answer = " "
			StartValue = StartValue * MultiplierValue
		Loop

	End Sub
End Class



jayman or anyone it still does not work.can you even further please
Was This Post Helpful? 0
  • +
  • -

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Numeric Validation

Posted 26 January 2007 - 11:43 AM

What error messages are you getting now?
Was This Post Helpful? 0
  • +
  • -

#11 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: Numeric Validation

Posted 26 January 2007 - 11:52 AM

OK, I just copied your code to my computer, created your txtboxes and button and ran the program.

You are apperantly having trouble with the same exact thing every time.

When you want to convert something in a TEXTBOX you must convert it using the VAL() constructor, this will then allow you to Convert to INTeger with CInt, as M2 stated, you need to do it like this,
StartValue = CType(val(txtStartValue.Text), Integer)
MaximumValue = CType(val(txtMaximumValue.Text), Integer)

'							 or 

StartValue = CInt(Val(txtStartValue.Text))
MaximumValue = CInt(Val(txtMaximumValue.Text))

'  would also work



You must do this every time you convert from TEXT to NUMERICAL
when we show you how to fix a basic error, it is up to you to find every where else it occurs in your program and fix it.
Was This Post Helpful? 0
  • +
  • -

#12 INeedHelpPlease  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-January 07

Re: Numeric Validation

Posted 26 January 2007 - 11:52 AM

im still gettin the same errors.is there any chance that i can send you a zip file through email or something if thats easier?
Was This Post Helpful? 0
  • +
  • -

#13 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Numeric Validation

Posted 26 January 2007 - 12:40 PM

Quote

you must convert it using the VAL()

You do not need to use the Val function when converting String values in VB.NET.

You only need to use the conversion function.

Conversion functions and examples.

If you have made all the recommended changes that have been posted by DIC members and it still does not work then zip up your project and upload as an attachment in a post in this topic.
Was This Post Helpful? 0
  • +
  • -

#14 INeedHelpPlease  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-January 07

Re: Numeric Validation

Posted 26 January 2007 - 12:44 PM

KeyWiz i tried that but it doesnt work.could you send me the code you put in please?did you do the test where you put o in and also letters and both rejected without problems?

Heres my program.Cheers

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#15 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Numeric Validation

Posted 26 January 2007 - 02:04 PM

The biggest problem is that you never assign a value to the MultiplierValue variable.

This is the code you currently have:
		If IsNumeric(txtMultiplierValue.Text) = True Then   'Checks if txtmultipliervalue is numeric

			 StartValue = CInt(Val(txtStartValue.Text))

		Else


You need to assign the value of txtMultiplierValue to your variable MultiplierValue. This is creating an infinite loop below in your calculation, since the default value stored in MultiplierValue is 0. This StartValue = StartValue * MultiplierValue will always be zero.
		If IsNumeric(txtMultiplierValue.Text) = True Then   'Checks if txtmultipliervalue is numeric

			MultiplierValue = CInt(Me.txtMultiplierValue.Text)

		Else


You are also going to have to handle if the user enters 1 for the multiplier. Anything times the value of 1 will never increase in value. So the minimum value your multiplier should be able to accept is 2.

Next set of problems is that when you are testing to see if the values are less than the 1. Your IF statement only prints a message to the user but execution of the rest of the code inside your click event will continue to execute causing your application to crash or get stuck in a loop.

So rather than having this:
		If IsNumeric(txtMultiplierValue.Text) = True Then   'Checks if txtmultipliervalue is numeric

			StartValue = CInt(Val(txtStartValue.Text))


		Else
			txtMultiplierValue.Clear()
			MessageBox.Show("Multiplier Value: Operation not valid") 'Messagebox Will Appear If Not Valid

		End If


		If CInt(Val(txtStartValue.Text)) < 1 Then
			MessageBox.Show("Start Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Else
			StartValue = CInt(Val(txtStartValue.Text))
		End If

		If CInt(Me.txtMaximumValue.Text) < 1 Then
			MessageBox.Show("Maximum Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Else
			MaximumValue = CInt(Val(txtMaximumValue.Text))

		End If

When the values are invalid for input you need to use Exit Sub to leave the subroutine and force the user to reenter the values.
		If IsNumeric(txtMultiplierValue.Text) = True Then   'Checks if txtmultipliervalue is numeric

			MultiplierValue = CInt(Me.txtMultiplierValue.Text)

		Else
			txtMultiplierValue.Clear()
			MessageBox.Show("Multiplier Value: Operation not valid") 'Messagebox Will Appear If Not Valid
			Exit Sub
		End If


		If CInt(Val(txtStartValue.Text)) < 1 Then
			MessageBox.Show("Start Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
			Exit Sub
		End If

		If CInt(Me.txtMaximumValue.Text) < 1 Then
			MessageBox.Show("Maximum Value: You Cannot Enter This Number") 'If Number Entered Less Than 1 Error Message Will Appear
		Exit Sub

		End If


You also seem to have some other issues with your project which are behind the scenes. You might want to create a new project add the controls and copy and paste your code into this new project. Once that is done everything seems to work just fine.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2