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

Join 137,234 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,644 people online right now. Registration is fast and FREE... Join Now!




Validation

 
Reply to this topicStart new topic

Validation, Validation in function

VisualBasicBeginner
13 Mar, 2007 - 03:17 PM
Post #1

New D.I.C Head
*

Joined: 4 Mar, 2007
Posts: 33


My Contributions
Hey guys,
I'm having trouble with input validation for my project. I'm trying to set the input validation in a function where the textbox (txtChevy.Text) does not accept negative amounts or letters. I gave it a shot, but when you run it, its calculating the negative numbers. Also, I need to set the focus back to the textbox and highlight the incorrect number so the user is able to fix it before proceeding. Thanks guys!

Function ChevroletCost()

Dim intCostOfChevrolet As Integer

If IsNumeric(txtChevy.Text) = False Then
MessageBox.Show("Enter a number", "Input Error")
txtChevy.Focus()
End If

If (intCostOfChevrolet < 0) Then
MessageBox.Show("Enter a positive number", "Input Error")
Else
intCostOfChevrolet = CInt(txtChevy.Text)
End If

Return intCostOfChevrolet
End Function

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Validation
13 Mar, 2007 - 08:51 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Refer to the following snippet for instructions on how to limit what can be entered into a textbox.
http://www.dreamincode.net/code/snippet522.htm

You already have the code to validate if it is less than zero. You just need to add a line to give focus back to the textbox that needs the user to reenter the information into.
CODE

If (Val(txtChevy.Text) < 0) Then
MessageBox.Show("Enter a positive number", "Input Error")
txtChevy.Focus()
Else
intCostOfChevrolet = CInt(txtChevy.Text)
End If

User is online!Profile CardPM
+Quote Post

VisualBasicBeginner
RE: Validation
14 Mar, 2007 - 11:08 AM
Post #3

New D.I.C Head
*

Joined: 4 Mar, 2007
Posts: 33


My Contributions
Thanks Jayman!! Ok, I am little confused. I fixed up the code a bit. The only thing I am having trouble with is when a user enters a negative number, it goes to the next textbox. So when I enter for example -1, it goes to the next textbox and I only get the error message when I hit the Calculate button. I am trying to make it where if a user accidentally enters a negative number, the error message appears, and the focus goes right back to the box and won't let you proceed on until its a positive number! Sorry about that!!

Function ChevroletCost()

Dim intCostOfChevrolet As Integer

If (Val(txtChevy.Text) < 0) Then
MessageBox.Show("Enter a positive number", "Input Error")
txtChevy.Focus()
Else
intCostOfChevrolet = CInt(txtChevy.Text)
End If

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Validation
14 Mar, 2007 - 11:15 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
In what method are you calling the function? The calculate button? or some sort of onBlur method?
User is online!Profile CardPM
+Quote Post

VisualBasicBeginner
RE: Validation
14 Mar, 2007 - 01:24 PM
Post #5

New D.I.C Head
*

Joined: 4 Mar, 2007
Posts: 33


My Contributions
Hi, I'm not sure what you mean by method. The only thing i called it was Function ChevroletCost(). It is for a textbox inside of a groupbox. The chevy numbers entered in the textbox by the user get calculated inside of a label in another groupbox when you press the calculate button. The label is called lblChevy.Text and the Dim i made for it was Dim decChevy As Decimal. This is my first time doing functions. Sorry for the confusion!
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Validation
14 Mar, 2007 - 02:29 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
QUOTE
So when I enter for example -1, it goes to the next textbox and I only get the error message when I hit the Calculate button.


Did you put the code for the snippet I referred to inside the KeyPress event of the textbox you are trying to limit input??

You should not be able to even enter the - key, it will be ignored.

If you post all of your code then I will be better able to help you. Then I can see what you are doing and where.
User is online!Profile CardPM
+Quote Post

m2s87
RE: Validation
14 Mar, 2007 - 05:28 PM
Post #7

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 1 times
Dream Kudos: 1225
My Contributions
Well you could use
CODE
If (Val(txtChevy.Text) < 0) Then
   MessageBox.Show("Enter a number", "Input Error")
   txtChevy.Focus()
Else
   Rem if you want demical places, change integer in Ctype converter to to some other type like single or double.
   if mid(txtChevy.Text,1,1)="-" then
      intCostOfChevrolet = CType(val(mid(txtChevy.Text,2,len(txtChevy.Text)-1)*-1,integer)
   else
      intCostOfChevrolet = CType(val(txtChevy.Text),integer)
   end if
End If


This post has been edited by m2s87: 14 Mar, 2007 - 05:30 PM
User is offlineProfile CardPM
+Quote Post

VisualBasicBeginner
RE: Validation
19 Mar, 2007 - 10:19 AM
Post #8

New D.I.C Head
*

Joined: 4 Mar, 2007
Posts: 33


My Contributions
Thank you to everyone for all of your help. I figured it out! Thanks again everyone!!! smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 04:01PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month