Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old April 14th, 2005, 06:44 AM
priyam priyam is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 2 priyam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 43 sec
Reputation Power: 0
Red face HELP!!!! Texbox validation

I have 2 textboxes both which you have to enter a temperature which includes minus numbers. I am new to VB6 and have been advised of validation and need to know what I can do to validate outsidetemp.text and insidetemp.text so only numbers can be entered. If the user tries to enter a character a pop up message should appear?

Thanks

Reply With Quote
  #2  
Old April 14th, 2005, 06:58 AM
musa's Avatar
musa musa is offline
Master MC
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: South Africa
Posts: 43 musa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 9 m 20 sec
Reputation Power: 5
Facebook
Wink

Can't remmber exactly, but for now try something like

Code:
 
If Not IsNumeric(outsidetemp.text) Then
MsgBox ("This is not a valid number.")
End If
If Not IsNumeric(insidetemp.text) Then
MsgBox ("This is not a valid number.")
End If

You need to make sure to do something whe this happens, like
setting the focus to the TextBox and then allowing the user to enter the number again before
you use it.
I think you need to use
Code:
 outsidetemp.focus ' set focus 
to set focus the this
textbox.
I'm hoping someone is gonna correct me if I'm wrong.

Reply With Quote
  #3  
Old April 14th, 2005, 07:00 AM
Phoenix's Avatar
Phoenix Phoenix is offline
Web-Standards Evangelist
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Posts: 1,522 Phoenix User rank is Corporal (100 - 500 Reputation Level)Phoenix User rank is Corporal (100 - 500 Reputation Level)Phoenix User rank is Corporal (100 - 500 Reputation Level)Phoenix User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 23 h 48 m 4 sec
Reputation Power: 9
AFAIK, VB6 doesn't have validation events, instead you need to use the lose_focus event.

Create an eventhandler for when either textbox loses focus, then write the validation code yourself and display an error if nessacery.

Note that unlike VB7, there is not "e.Cancel = False" to prevent the control losing focus.

For example:

Code:
Private Sub outsidetemp_losefocus()
	TempValidate(outsidetemp)
End Sub
Private Sub insidetemp_losefocus()
	TempValidate(insidetemp)
End Sub
Private Sub TempValidate(textbox)
	If textbox.Text Like "[A-zA-Z]" Then
	   Msgbox("Please enter a valid number please")
	End If
End Sub


Something like that.

Reply With Quote
  #4  
Old April 14th, 2005, 07:13 AM
musa's Avatar
musa musa is offline
Master MC
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: South Africa
Posts: 43 musa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 9 m 20 sec
Reputation Power: 5
Facebook
Cool View this document

Quote:
Originally Posted by priyam
I have 2 textboxes both which you have to enter a temperature which includes minus numbers. I am new to VB6 and have been advised of validation and need to know what I can do to validate outsidetemp.text and insidetemp.text so only numbers can be entered. If the user tries to enter a character a pop up message should appear?

Thanks

This docuemnt might help you. you need to have PDF reader/viewer though.
http://www.hitmill.com/programming/vb/validation.pdf

Reply With Quote
  #5  
Old April 14th, 2005, 07:46 AM
priyam priyam is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 2 priyam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 43 sec
Reputation Power: 0
Smile

thanks guys...I am going to try the stuff you have given and get back to you!

Reply With Quote
  #6  
Old April 27th, 2005, 06:52 AM
Marc Van Beeume Marc Van Beeume is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 6 Marc Van Beeume User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 6 m 53 sec
Reputation Power: 0
hi

Just wanted to inform you that i read al the offered solutions, even the pdf file solution. I have to tell you that all of them come pretty close but none of them solves the problem completely.

Th most important remaining problem is the decimal separator. If you use . as decimal separator the following value 45,7 is still a valid nummer with all the solutions that were offered. the actual value will then be truncated to 45 which isn't what you want is it ?

The code beneath takes care of a few extra possible errors, namely :
- removing leading and trailing blancs
- conversing , to . as decimal symbol
- removing eventual blancs between the minus sign and the valuie itself

Here is the code....wish you good luck


Private Sub TxtTemperature_LostFocus()

Dim TemperatureValue As Double

'Remove the leading and trailing blancs
TxtTemperature.Text = Trim(TxtTemperature.Text)
'Replace the , with a . (depending which decimal sign you are using)
TxtTemperature.Text = Replace(TxtTemperature.Text, ",", ".")
'Replace eventual blanks between the minus sign and the value
TxtTemperature.Text = Replace(TxtTemperature.Text, " ", "")

'Check if the return value of the control is false
If CheckValue(TxtTemperature.Text) = False Then
'Give error message
MsgBox "Not a correct value", vbCritical, "No valid temperature"
'Select the typed in text so you can start retyping directly, replacing what as there before
TxtTemperature.SelStart = 0
TxtTemperature.SelLength = Len(TxtTemperature.Text)
'Put the focus back onto the temperature field
TxtTemperature.SetFocus
End If

End Sub


Private Function CheckValue(ControlNumber As String) As Boolean

Dim Position As Integer
Dim CaracterCode As Integer

'Initialize the return code
CheckValue = True

'Initialize the Position to the first position in the string or the second depending on the fact
'if the leftmost caracter is a minus sign or not
If Left(ControlNumber, 1) = "-" Then
Position = 2
Else
Position = 1
End If

'Check if the remaining caracters are numbers or the decimal separator .
Do While Position < Len(ControlNumber)
Position = Position + 1
CaracterCode = Asc(Mid(ControlNumber, Position, 1))
'Illegal caracter, quit the loop and set the return code to false
If Not ((CaracterCode >= Asc("0") And CaracterCode <= Asc("9")) Or CaracterCode = Asc(".")) Then
CheckValue = False
Exit Do
End If
Loop
End Function

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > HELP!!!! Texbox validation


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT