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?
Hi,
You can try the following Code - 'Add This Function to your Code
-
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)
-
Dim KeyAscii As Integer
-
Dim intCounter As Integer
-
Dim EnteredKeyAscii As Integer
-
Dim mValidCharacters As String = ""
-
Try
-
If e.KeyChar = "'" Then
-
e.KeyChar = CChar("".ToString)
-
Exit Sub
-
End If
-
-
KeyAscii = Asc(e.KeyChar)
-
EnteredKeyAscii = Asc(e.KeyChar)
-
-
If blnNumeric = True And blnAlpha = False And blnSpecial = True And strAllowableChars = "." And strDisAllowableChars = "" Then
-
If e.KeyChar = "." Then
-
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
-
e.KeyChar = CChar("".ToString)
-
Exit Sub
-
End If
-
End If
-
End If
-
-
If blnToUpperCase = True Then KeyAscii = KeyAscii - CInt(IIf(KeyAscii >= 97 And KeyAscii <= 122, 32, 0))
-
-
If blnAlpha = True Then
-
For intCounter = Asc("A") To Asc("Z")
-
mValidCharacters = mValidCharacters & UCase(Chr(intCounter))
-
Next
-
End If
-
-
If blnNumeric = True Then
-
For intCounter = 0 To 9
-
mValidCharacters = mValidCharacters & intCounter
-
Next
-
End If
-
-
If blnSpecial = True Then
-
If strAllowableChars <> "" And strDisAllowableChars <> "" Then
-
mValidCharacters = mValidCharacters & strAllowableChars
-
ElseIf strAllowableChars <> "" And strDisAllowableChars = "" Then
-
mValidCharacters = mValidCharacters & strAllowableChars
-
ElseIf strAllowableChars = "" And strDisAllowableChars <> "" Then
-
For intCounter = 1 To 255
-
If Not InStr(1, strDisAllowableChars, Chr(intCounter), vbTextCompare) > 0 Then
-
mValidCharacters = mValidCharacters & Chr(intCounter)
-
End If
-
Next
-
ElseIf strAllowableChars = "" And strDisAllowableChars = "" Then
-
For intCounter = 1 To 255
-
mValidCharacters = mValidCharacters & Chr(intCounter)
-
Next
-
End If
-
End If
-
-
If Not (InStr(1, mValidCharacters, Chr(KeyAscii).ToString, CompareMethod.Text)) = 0 Or KeyAscii = 8 Then
-
e.KeyChar = Chr(KeyAscii)
-
Else
-
e.KeyChar = CChar("".ToString)
-
End If
-
Catch excp As Exception
-
MsgBox(excp.Message)
-
End Try
-
End Sub
-
-
'How to use - Example
-
ValidateKeys(txtEmpID.Text, e, False, True, False)
Regards
Vijay.R
2 2104
Hi,
You can try the following Code - 'Add This Function to your Code
-
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)
-
Dim KeyAscii As Integer
-
Dim intCounter As Integer
-
Dim EnteredKeyAscii As Integer
-
Dim mValidCharacters As String = ""
-
Try
-
If e.KeyChar = "'" Then
-
e.KeyChar = CChar("".ToString)
-
Exit Sub
-
End If
-
-
KeyAscii = Asc(e.KeyChar)
-
EnteredKeyAscii = Asc(e.KeyChar)
-
-
If blnNumeric = True And blnAlpha = False And blnSpecial = True And strAllowableChars = "." And strDisAllowableChars = "" Then
-
If e.KeyChar = "." Then
-
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
-
e.KeyChar = CChar("".ToString)
-
Exit Sub
-
End If
-
End If
-
End If
-
-
If blnToUpperCase = True Then KeyAscii = KeyAscii - CInt(IIf(KeyAscii >= 97 And KeyAscii <= 122, 32, 0))
-
-
If blnAlpha = True Then
-
For intCounter = Asc("A") To Asc("Z")
-
mValidCharacters = mValidCharacters & UCase(Chr(intCounter))
-
Next
-
End If
-
-
If blnNumeric = True Then
-
For intCounter = 0 To 9
-
mValidCharacters = mValidCharacters & intCounter
-
Next
-
End If
-
-
If blnSpecial = True Then
-
If strAllowableChars <> "" And strDisAllowableChars <> "" Then
-
mValidCharacters = mValidCharacters & strAllowableChars
-
ElseIf strAllowableChars <> "" And strDisAllowableChars = "" Then
-
mValidCharacters = mValidCharacters & strAllowableChars
-
ElseIf strAllowableChars = "" And strDisAllowableChars <> "" Then
-
For intCounter = 1 To 255
-
If Not InStr(1, strDisAllowableChars, Chr(intCounter), vbTextCompare) > 0 Then
-
mValidCharacters = mValidCharacters & Chr(intCounter)
-
End If
-
Next
-
ElseIf strAllowableChars = "" And strDisAllowableChars = "" Then
-
For intCounter = 1 To 255
-
mValidCharacters = mValidCharacters & Chr(intCounter)
-
Next
-
End If
-
End If
-
-
If Not (InStr(1, mValidCharacters, Chr(KeyAscii).ToString, CompareMethod.Text)) = 0 Or KeyAscii = 8 Then
-
e.KeyChar = Chr(KeyAscii)
-
Else
-
e.KeyChar = CChar("".ToString)
-
End If
-
Catch excp As Exception
-
MsgBox(excp.Message)
-
End Try
-
End Sub
-
-
'How to use - Example
-
ValidateKeys(txtEmpID.Text, e, False, True, False)
Regards
Vijay.R
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?
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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;
}
|
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
|
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,
|
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...
|
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...
|
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
|
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?
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |