Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numeric Validation in a Text Box

Status
Not open for further replies.

Rougy

Programmer
Oct 15, 2001
249
0
0
US
Hi,

This is probably an easy one, but I haven't found the answer yet without writing a special routine.

Is there a quick way to validate numeric-only input in a text box?

Rougy
 
not a 'quick' way necessarily

Public Sub NumericLimiter(Which_Box As TextBox)
'This sub limits the entries made to the parameter text box to numeric values > 0

Dim intCursor_Position As Integer

If Not Which_Box.Text = "" Then
If IsNumeric(Which_Box.Text) = False Then
'If extra entry to text box is not numeric then remove it from where it was inserted
intCursor_Position = Which_Box.SelStart
With Which_Box
.Text = Left(Which_Box.Text, (intCursor_Position - 1)) + Right(Which_Box.Text, (Len(Which_Box.Text) - intCursor_Position))
.SelStart = (intCursor_Position - 1)
.SelLength = 0
End With
Exit Sub
Else
If Which_Box.Text = "0" Then
Which_Box.Text = ""
Exit Sub
End If
End If
End If
End Sub
 
In the lost focus routine you could do the following:

Sub Textbox_LostFocus

dim NumValue as Integer

NumValue = val(textbox.text)
if (Trim(NumValue) <> Trim(textbox.text)) then
msgbox &quot;Please Enter a Numeric Value
Textbox.SetFocus
endif

End sub
Good Luck
------------
Select * from Users where Clue > 0
0 rows returned
 
Or the more flexible (it deals with regional settings, e.g decimal seperators and digit grouping characters):
[tt]
Sub Text1_LostFocus()

If Not IsNumeric(Text1.Text) Then
MsgBox &quot;Please Enter a Numeric Value&quot;
Text1.SetFocus
End If

End Sub
[/tt]
There are a couple of oddities that you might encounter with IsNumeric (e.g: 6,5- returns true with UK regional settings since the , is treated as a digit grouping character even though it is not in the traditinal place, and trailinging mathematical symbols are ignored)
 
CajunCenturion, I would do this in the Validation event and not the lost focus, change NumValue to a long, and use the IsNumeric function and not the Val function (only works if a decimal is in US format - for example, German format for .22 is ,22 so the Val will always return 0 there)

The best way I have found to do this is to add code in the keypress event of the text box:
You will need to check the KeyAscii for the values
vbKeyBack, vbKeyClear, vbKey0 to vbKey9, 45 for a minus sign, 44 for a comma, and 46 for a dot.

If it is none of these then set KeyAscii = 0 to cancel input.

Then check for IsNumeric in case more than one dot, comma or minus is entered:


Private Sub TextBox1_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case vbKeyBack,vbKeyClear,45,44,46
Case vbKey0 to vbKey9
Case else
KeyAscii = 0
End Select

If Not IsNumeric(TextBox1.Text & Chr$(KeyAscii) then KeyAscii =0
End IF

End Sub

Add an additional check in the Validation event in case something went through:

Private Sub TextBox1_Validate(Cancel As Boolean)
If Not IsNumeric(TextBox1.Text) Then
'Error
MsgBox &quot;Enter a Number&quot;
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
Else
'In case more than 1 period is entered
TextBox1.Text = CDbl(TextBox1.Text)
End If
End Sub


I would create 2 functions out of all this so you do not have to repeat so much code everywhere (pass the KeyAscii ByRef,and the current TextBox Text ByVal.
 
good point Clint, I'm not in the habit of thinking internationally, but it would be a good practice.

Good Luck
------------
Select * from Users where Clue > 0
0 rows returned
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top