How to filter textbox input to numeric only?

前端 未结 9 1459
耶瑟儿~
耶瑟儿~ 2020-12-21 13:37

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
          


        
相关标签:
9条回答
  • 2020-12-21 14:06

    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>
    
    0 讨论(0)
  • 2020-12-21 14:07

    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
    
    0 讨论(0)
  • 2020-12-21 14:12
     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
    
    0 讨论(0)
  • 2020-12-21 14:14

    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
    
    0 讨论(0)
  • 2020-12-21 14:23

    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 ..

    0 讨论(0)
  • 2020-12-21 14:25

    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
    
    0 讨论(0)
提交回复
热议问题