473,461 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A function to only allow numeric input in a textbox

mshmyob
904 Expert 512MB
Here is a way to limit user input in a textbox to only numeric characters.

I have created a function and I put mine in a Class module that holds my custom functions. The function is called from the KEYPRESS event of any textbox control where you want to limit to numeric input.

The only parameter you may wish to change is the last parameter which determines the number of digits to the right of the decimal place. This sample code limits the input to 2 decimal places.

Code for KEYPRESS event

Expand|Select|Wrap|Line Numbers
  1. Dim myTextBox As TextBox
  2. myTextBox = Me.ActiveControl
  3. If myMiscClass.myNumOnly(e.KeyChar, Me.ActiveControl.Text, myTextBox.SelectionStart, 2) = True Then
  4.         e.Handled = True
  5. Else
  6.         e.Handled = False
  7.  End If
  8. If e.KeyChar = Chr(13) Then GetNextControl(Me.ActiveControl, True).Focus() 'Enter key moves to next control
  9.  
Code for Function (I put this in it's own class module (so I only need it once) but you may put it in your form module if you wish.)

Expand|Select|Wrap|Line Numbers
  1. '*****************************************************
  2. ' myNumOnly()
  3. ' Purpose: Limits textbox input to numeric only
  4. ' Inputs:   strKeyPress, key pressed
  5. '           strText, current text in textbox
  6. '           intPosition, current cursor position in textbox
  7. '           intDecimal, number of decimal places required
  8. '       ' Returns: False/True - discard keystroke/valid key
  9. '*****************************************************
  10. Shared Function myNumOnly(ByVal strKeyPress As String, ByVal strText As String, ByVal intPosition As Integer, ByVal intDecimal As Integer) As Boolean
  11.  
  12. Dim dot As Integer, ch As String
  13.  
  14. If Not Char.IsDigit(strKeyPress) Then myNumOnly = True
  15. If strKeyPress = "-" And intPosition = 0 Then myNumOnly = False 'allow negative number
  16. If strKeyPress = "." And strText.IndexOf(".") = -1 Then myNumOnly = False 'allow single decimal point
  17. dot = strText.IndexOf(".")
  18. If dot > -1 Then 'limit to set decimal places
  19.        ch = strText.Substring(dot + 1)
  20.        If ch.Length > (intDecimal - 1) Then myNumOnly = True
  21. End If
  22. If strKeyPress = Chr(8) Then myNumOnly = False 'allow Backspace
  23. Return myNumOnly
  24. End Function
  25.  
Feel free to ask any questions.

cheers,
Mar 10 '10 #1
1 11730
yarbrough40
320 100+
Thanks for this! I have written it in a notepad for posterity.
Mar 11 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
9
by: Rowan Chapman | last post by:
Hey all! I'm kinda new to VB but not to programin'. So I know what it is like when you are asked trivial questions. Could some1 please tell me what the syntax would be 2 only allow numerical data...
3
by: Craig | last post by:
I currently have the following code: <input name="castStation" type="text" value="0" onChange="valueCalculate()";> The function called does a few caluclations and writes the total to a total...
1
by: Jose Gonzalez | last post by:
How to apply a numeric format to a textbox using xhtml? I know you have to use the "-wap-input-format" style tag in css. I can get this to work in a regular xhtml page, however, I've been...
2
by: Sam | last post by:
Why isn't there a numberBox Windows Form control like a textbox control but for numeric (float) input? There doesn't seem to be any easy or visual solution to this VERY SIMPLE ISSUE in either...
5
by: MrMike | last post by:
Hi. I'm trying to create a simple validation process that only allows a user to enter a numeric value into a textbox. However, my process does not work and the user can still enter non-numeric...
16
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work...
4
by: viv | last post by:
how do you trap keyboard codes for text box in VB2005 ? I'm looking for a code snippet to only allow numeric input in a text box (same as Key event in VB6, then use Keyascii to decide action) ...
3
by: shyamg | last post by:
hi all, This javascript is working IE but not working in FIreFox, validating text fields. var dealerid = new keybEdit('abcdefghijklmnopqurstuvwxyz01234567890 ','Alpha-numeric input only.'); ...
3
LoanB
by: LoanB | last post by:
Hi I'm looking to ensure that my textbox only accepts numbers .. AND ... mathematical operators + - . / *. an dmaybe a blank space I can ensure that only number are included by using: ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.