473,503 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A function to only allow numeric input in a textbox

mshmyob
904 Recognized Expert Contributor
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 11754
yarbrough40
320 Contributor
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
4935
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
10416
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
8636
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
4628
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
5534
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
4655
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
8335
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
6923
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
2452
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
3388
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: ...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
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...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
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
3177
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...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.