How do I suppress all data except numeric?
This is not working on KeyDown()
:
If e.KeyData < Keys.D0 Or e.KeyData > Keys.D9 Then
I suggest that you use regular expressions. You can search Google, like 'regular expression textbox only numeric' and I guess you'll come up with many examples.
For example, if you are in ASP.NET you can do it like:
<asp:TextBox
ID="txtPhoneNumber"
runat="server"
Text='<%#Bind("phoneNumber") %>'
MaxLength="15">
</asp:TextBox>
<asp:RegularExpressionValidator
ID="rfvUSerPhoneNumberValidate"
runat="server"
ControlToValidate="txtPhoneNumber"
Display="Dynamic"
ValidationExpression="^[0-9]{1,15}$"
ErrorMessage="Please enter only numeric value for Phone Number"
EnableViewState="true">
</asp:RegularExpressionValidator>
There are many ways to do this. I've had a quick stab at it and go this which works. I have used the KeyPress sub for the textbox, and pass each keypress to the IsNumber function.
NOTE: I have allowed the backspace key to be used in case you make a mistake with the numbers and want to deleted.
Take out the If e.KeyChar <> ChrW(Keys.Back) Then / End If part if you dont need the backspace.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar <> ChrW(Keys.Back) Then
If Char.IsNumber(e.KeyChar) Then
Else
e.Handled = True
End If
End If
End Sub
Public Class NumericTextBox : Inherits System.Windows.Forms.TextBox
Protected Overrides Sub OnKeyPress(e As Windows.Forms.KeyPressEventArgs)
If Char.IsDigit(e.KeyChar) Or
Char.IsControl(e.KeyChar) Or
e.KeyChar = lobalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator Then
MyBase.OnKeyPress(e)
Else
e.Handled = True
End If
End Sub
End Class
This code will help you to restrict multiple TEXTBOX to accept only NUMERIC VALUE and BACKSPACE key. However you can remove If e.KeyChar <> ChrW(Keys.Back) Then and End If value from code when you don't want to accept backspace key. Enhanced version of the kevchadders solution in this thread.
Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress
If e.KeyChar <> ChrW(Keys.Back) Then
If Char.IsNumber(e.KeyChar) Then
Else
e.Handled = True
End If
End If
End Sub
This is another way to restrict number inputs into textbox . using KEYPRESS Events
If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then MessageBox.Show("Only Numbers") e.Handled = True End If End Sub hope it helps ! thnks ..
You can check Char.IsDigit(e.KeyChar), but the best thing to do in this case is to create a subclass of TextBox and override IsInputChar(). That way you have a reusable TextBox control that you can drop anywhere so you don't have to re-implement the logic.
(My VB is a bit rusty...)
Public Class NumericTextBox : Inherits TextBox
Protected Overrides Function IsInputChar(Byval charCode As Char) As Boolean
If (Char.IsControl(charCode) Or Char.IsDigit(charCode)) Then
Return MyBase.IsInputChar(charCode)
Else
Return False
End If
End Function
End Class