473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How can I use numeric values only but also allow the Backspace?

I am using Visual Basic Express 2008.
I have added the following code to my text box:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Not IsNumeric(e.KeyChar) Then
MessageBox.Show("Please insert numbers only.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
e.KeyChar = Nothing
End If
End Sub

The problem is that if I click Backspace it shows the same error and I can't change a type error. Is there a way to get it working?
Mar 31 '11 #1

✓ answered by VijaySofist

Hi,

You can try the following Code

Expand|Select|Wrap|Line Numbers
  1. 'Add This Function to your Code
  2. Public Shared Sub ValidateKeys(ByVal strContent As String, ByVal e As System.Windows.Forms.KeyPressEventArgs, ByVal blnAlpha As Boolean, ByVal blnNumeric As Boolean, ByVal blnSpecial As Boolean, Optional ByVal strAllowableChars As String = "", Optional ByVal strDisAllowableChars As String = "", Optional ByVal blnToUpperCase As Boolean = True)
  3.         Dim KeyAscii As Integer
  4.         Dim intCounter As Integer
  5.         Dim EnteredKeyAscii As Integer
  6.         Dim mValidCharacters As String = ""
  7.         Try
  8.             If e.KeyChar = "'" Then
  9.                 e.KeyChar = CChar("".ToString)
  10.                 Exit Sub
  11.             End If
  12.  
  13.             KeyAscii = Asc(e.KeyChar)
  14.             EnteredKeyAscii = Asc(e.KeyChar)
  15.  
  16.             If blnNumeric = True And blnAlpha = False And blnSpecial = True And strAllowableChars = "." And strDisAllowableChars = "" Then
  17.                 If e.KeyChar = "." Then
  18.                     If strContent.IndexOf(".") >= 0 Or strContent.Length.Equals(0) Then 'if period already exists or if the user has entered period as the first entry
  19.                         e.KeyChar = CChar("".ToString)
  20.                         Exit Sub
  21.                     End If
  22.                 End If
  23.             End If
  24.  
  25.             If blnToUpperCase = True Then KeyAscii = KeyAscii - CInt(IIf(KeyAscii >= 97 And KeyAscii <= 122, 32, 0))
  26.  
  27.             If blnAlpha = True Then
  28.                 For intCounter = Asc("A") To Asc("Z")
  29.                     mValidCharacters = mValidCharacters & UCase(Chr(intCounter))
  30.                 Next
  31.             End If
  32.  
  33.             If blnNumeric = True Then
  34.                 For intCounter = 0 To 9
  35.                     mValidCharacters = mValidCharacters & intCounter
  36.                 Next
  37.             End If
  38.  
  39.             If blnSpecial = True Then
  40.                 If strAllowableChars <> "" And strDisAllowableChars <> "" Then
  41.                     mValidCharacters = mValidCharacters & strAllowableChars
  42.                 ElseIf strAllowableChars <> "" And strDisAllowableChars = "" Then
  43.                     mValidCharacters = mValidCharacters & strAllowableChars
  44.                 ElseIf strAllowableChars = "" And strDisAllowableChars <> "" Then
  45.                     For intCounter = 1 To 255
  46.                         If Not InStr(1, strDisAllowableChars, Chr(intCounter), vbTextCompare) > 0 Then
  47.                             mValidCharacters = mValidCharacters & Chr(intCounter)
  48.                         End If
  49.                     Next
  50.                 ElseIf strAllowableChars = "" And strDisAllowableChars = "" Then
  51.                     For intCounter = 1 To 255
  52.                         mValidCharacters = mValidCharacters & Chr(intCounter)
  53.                     Next
  54.                 End If
  55.             End If
  56.  
  57.             If Not (InStr(1, mValidCharacters, Chr(KeyAscii).ToString, CompareMethod.Text)) = 0 Or KeyAscii = 8 Then
  58.                 e.KeyChar = Chr(KeyAscii)
  59.             Else
  60.                 e.KeyChar = CChar("".ToString)
  61.             End If
  62.         Catch excp As Exception
  63.             MsgBox(excp.Message)
  64.         End Try
  65.     End Sub
  66.  
  67. 'How to use - Example
  68. ValidateKeys(txtEmpID.Text, e, False, True, False)
Regards
Vijay.R

2 2104
VijaySofist
107 100+
Hi,

You can try the following Code

Expand|Select|Wrap|Line Numbers
  1. 'Add This Function to your Code
  2. Public Shared Sub ValidateKeys(ByVal strContent As String, ByVal e As System.Windows.Forms.KeyPressEventArgs, ByVal blnAlpha As Boolean, ByVal blnNumeric As Boolean, ByVal blnSpecial As Boolean, Optional ByVal strAllowableChars As String = "", Optional ByVal strDisAllowableChars As String = "", Optional ByVal blnToUpperCase As Boolean = True)
  3.         Dim KeyAscii As Integer
  4.         Dim intCounter As Integer
  5.         Dim EnteredKeyAscii As Integer
  6.         Dim mValidCharacters As String = ""
  7.         Try
  8.             If e.KeyChar = "'" Then
  9.                 e.KeyChar = CChar("".ToString)
  10.                 Exit Sub
  11.             End If
  12.  
  13.             KeyAscii = Asc(e.KeyChar)
  14.             EnteredKeyAscii = Asc(e.KeyChar)
  15.  
  16.             If blnNumeric = True And blnAlpha = False And blnSpecial = True And strAllowableChars = "." And strDisAllowableChars = "" Then
  17.                 If e.KeyChar = "." Then
  18.                     If strContent.IndexOf(".") >= 0 Or strContent.Length.Equals(0) Then 'if period already exists or if the user has entered period as the first entry
  19.                         e.KeyChar = CChar("".ToString)
  20.                         Exit Sub
  21.                     End If
  22.                 End If
  23.             End If
  24.  
  25.             If blnToUpperCase = True Then KeyAscii = KeyAscii - CInt(IIf(KeyAscii >= 97 And KeyAscii <= 122, 32, 0))
  26.  
  27.             If blnAlpha = True Then
  28.                 For intCounter = Asc("A") To Asc("Z")
  29.                     mValidCharacters = mValidCharacters & UCase(Chr(intCounter))
  30.                 Next
  31.             End If
  32.  
  33.             If blnNumeric = True Then
  34.                 For intCounter = 0 To 9
  35.                     mValidCharacters = mValidCharacters & intCounter
  36.                 Next
  37.             End If
  38.  
  39.             If blnSpecial = True Then
  40.                 If strAllowableChars <> "" And strDisAllowableChars <> "" Then
  41.                     mValidCharacters = mValidCharacters & strAllowableChars
  42.                 ElseIf strAllowableChars <> "" And strDisAllowableChars = "" Then
  43.                     mValidCharacters = mValidCharacters & strAllowableChars
  44.                 ElseIf strAllowableChars = "" And strDisAllowableChars <> "" Then
  45.                     For intCounter = 1 To 255
  46.                         If Not InStr(1, strDisAllowableChars, Chr(intCounter), vbTextCompare) > 0 Then
  47.                             mValidCharacters = mValidCharacters & Chr(intCounter)
  48.                         End If
  49.                     Next
  50.                 ElseIf strAllowableChars = "" And strDisAllowableChars = "" Then
  51.                     For intCounter = 1 To 255
  52.                         mValidCharacters = mValidCharacters & Chr(intCounter)
  53.                     Next
  54.                 End If
  55.             End If
  56.  
  57.             If Not (InStr(1, mValidCharacters, Chr(KeyAscii).ToString, CompareMethod.Text)) = 0 Or KeyAscii = 8 Then
  58.                 e.KeyChar = Chr(KeyAscii)
  59.             Else
  60.                 e.KeyChar = CChar("".ToString)
  61.             End If
  62.         Catch excp As Exception
  63.             MsgBox(excp.Message)
  64.         End Try
  65.     End Sub
  66.  
  67. 'How to use - Example
  68. ValidateKeys(txtEmpID.Text, e, False, True, False)
Regards
Vijay.R
Mar 31 '11 #2
Wow :) Actually thanks a lot for the code!! But the only things I'd like to be available to insert should be 0123456789 Backspace Delete
Could you help me on how to do that?
Apr 2 '11 #3

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

Similar topics

2
by: qazmlp | last post by:
I have got the following strings: 121721234567890 13 1217212345 129 Is it possible to do descending sort for these strings based on their numeric values? After sorting, they need to figure...
3
by: success_ny | last post by:
Does anyone have a code snippet to compare those values so I can sort the array of alpha-numeric values that include both characters and integers in it? I.e., if we have values like 4236 and...
4
by: vighnesh | last post by:
Hi All I am presently dealing with a project,in which I have to convert an XLS file into XML file. To accomplish this I have used Dataset as intermediatory to store the data from XLS and...
3
by: Mike L | last post by:
In a text box I want to allow only numbers and backspace. Currently the code I have only allows numbers. How do I also allow backspace? if (!Char.IsDigit(e.KeyChar)) { e.Handled = true; }
6
by: meenu_susi | last post by:
hi.. i have a text box..where user input their value. but i want to allow only numeric values not alphabets and blankspaces.. how to do this in asp.. regards meenu
3
by: Krish | last post by:
I want to have an enum that has a list of numeric values. How do I achieve this? Something like this ... Public Enum AllowedNumbers { 111, 222, 333,
1
by: pchadha20 | last post by:
How to single numeric values from a field in a table which has multiple numeric value in a field of a table.And that table contains thousands of records. Suppose we have a table customer having...
7
by: nussu | last post by:
Hi, Plz provide me javascript : javascript for a textbox to accept the values in decimals and within range i need to enter a value in textbox like 1.03 and it should be <3 (lessthan 3). Plz...
1
by: Ronald S. Cook | last post by:
We have a table "Animal" that contains the column "AnimalNumber" with values like these: 100001 100002 100003 1XAB 10E43ABC 11RU5V
0
by: chaitanya83 | last post by:
I need to ensure that only numeric values can be obtained from the user.The non numeric values from the user should throw a validation error.How can this be done using validators in JSF?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.