Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Jul 23, 1999

Roll-Your-Own Decimal Entry Filter

Here's an easy method for making sure your users enter only numeric data, and only one decimal point. First, place two Public procedures in a standard module. You can use Private procedures in a form if you're only using it there, but you'll lose easy portability for future projects.

The first procedure makes sure the decimal point is only entered once. The second procedure filters out all non-numeric characters except the decimal point:

 
Public Sub DecCheck(Target As String, ByRef KeyStroke As _
	Integer)
	If InStr(Target, ".") And KeyStroke = 46 Then
		KeyStroke = 0
	End If
End Sub

Public Sub NumCheck(ByRef KeyStroke As Integer)
	If (KeyStroke < 48 Or KeyStroke > 57) And (KeyStroke _
		<> 46 And KeyStroke <> 8) Then
		KeyStroke = 0
	End If
End Sub

Then invoke the code from your TextBox's KeyPress event:

 
Private Sub txtUnitPrice_KeyPress(KeyAscii As Integer)
	DecCheck txtUnitPrice, KeyAscii
	NumCheck KeyAscii
End Sub

One caveat: This code doesn't prevent text characters from being pasted in via the clipboard.

Ron Schwarz
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date