Community discussion forum

Accept only numbers in a text field

This is a comment thread discussing Accept only numbers in a text field
  • 9 years ago

    This thread is for discussions of Accept only numbers in a text field.

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 6 years ago

    Keypress can also be used for this.


    Code:

    Private Sub Text1_KeyPress(KeyAscii as Integer)
     'Ignore negatives
     If KeyAscii = 45 then goto EndProc
     'If Key pressed is not numeric then make key Null
     If (KeyAscii < 48) or (Keyascii >57) then KeyAscii = 0
     Endproc:
    End Sub


    I use this myself.

  • 5 years ago

    KeyPress event is the better way to test user input. However, it requires a more elaborate code for user input testing.


    This example shows a moving decimal point and toggling for negative sign. Notice that whenever the control's value is changed by the code, the cursor position as to be reset.


    Private Sub Text1_KeyPress(KeyAscii As Integer)
      KeyAscii = InputNum(Text1, KeyAscii)
    End Sub


    Function InputNum(txtBox As TextBox, KeyAscii As Integer) As Integer
      If KeyAscii <> 8 Then ' Skips if Backspace key was used
         If InStr("01234567.-", Chr(KeyAscii)) = 0 Then
            KeyAscii = 0 ' Cancels user input
         ElseIf Chr(KeyAscii) = "." And InStr(Text1, ".") <> 0 Then
            a = Text1.SelStart
            b = InStr(Text1, ".")
            If a = b Or a + 1 = b Then ' there one already there
               KeyAscii = 0
            ElseIf a + 1 < b Then ' fiddle with "move" the decimal point
               Text1 = Left(Text1, b - 1) + Mid(Text1, b + 1)
               Text1.SelStart = a
            Else
               Text1 = Left(Text1, b - 1) + Mid(Text1, b + 1)
               Text1.SelStart = a - 1
            End If
         ElseIf Chr(KeyAscii) = "-" Then ' make/toggle the negative"
            a = Text1.SelStart
            KeyAscii = 0
            If InStr(Text1, "-") = 0 Then
               Text1 = "-" & Text1
               a = a + 1
            Else
               Text1 = Mid(Text1, 2)
               a = a - 1
            End If
            Text1.SelStart = a
         End If
      End If
      InputNum = KeyAscii
    End Function


  • 3 years ago

    Public Function CheckNumeric (strTest As String) As String


    Dim I%


    On Error GoTo NotNumeric
    I% = strTest
    CheckNumeric = strTest
    Exit Function


    NotNumeric:
    CheckNumeric= Left$(strTest, Len(strTest) - 1)


    End Function


    Add this code to a TextBox's Change event:


    Text1.Text = CheckNumeric(Text1.Text)

  • 1 year ago

    Limit input in textbox that allows only digits, backspaces and one decimal point to be entered .

    Method 1 using keypress event:
    Private sub Text1_KeyPress(KeyAscii As Integer)
        Const Numbers$ = "0123456789."      'Permitted values in Textbox
        
        If KeyAscii <> 8 Then                          'Ascii for BackSpace 8
            If InStr(Numbers, Chr(KeyAscii)) = 0 Then    'If false, keypressed is suppressed
                MsgBox "error"
                KeyAscii = 0
                Exit Sub
            end If
        end If
       
    End Sub
    ------------------------------
    Method 2 using function-call:
    Function LimitTextInput(source) as String
        Const Numbers$ = "0123456789."
       If source <> 8 Then  'backspace is 8
            If InStr(Numbers, Chr(source)) = 0 Then
                LimitTextInput = 0
                exit Function
            end If
        end If
        LimitTextInput = source
       End Function























    Private Sub Text1_KeyPress(KeyAscii As Integer)
     KeyAscii = LimitTextInput(KeyAscii)   'Calling LimitTextInput function
    End Sub
    ------------------------------
    Method 3 simple keypress event:
    Private sub Text1_KeyPress(KeyAscii As Integer)
     If KeyAscii > 65 And KeyAscii < 123 Then
        KeyAscii = 0
        End If
    End if
    -------------------------------









    Method 4 using Validate event:

    Private Sub Text1_Validate(Cancel As Boolean)

    Cancel = IIF(Not IsNumeric(Text1), True, False)

    If Cancel then

    Msgbox "Enter Numeric Value only!", VbInformation, "Entry Error"

    With Text1

       . text = " "

       .SetFocus

    End With

    End if

    End Sub
    In this event, the user can not exit textbox unless inputting Numericvalue and also all other controls in the Form will be disabled.

    Method 4 One More Example

    Private Sub Text1_Validate(Cancel As Boolean)
    Cancel = Not IsDate(Text1.Text) Or Text1.Text <> Format(Text1.Text, "hh:mm:ss")

    If Cancel Then
    With lblSetTime
        .Caption = "Set Time in 24hr Format!"
        .ForeColor = vbRed
    End With
      Text1.Text = ""
      Text1.SetFocus
      CmdStart.Enabled = False
    Else
      CmdStart.Enabled = True








    End If
    End Sub

    In the above example, user is forced to enter time in "hh:mm:ss" format only for use in setting Alarm Time. Entries of anything else will be curbed by Validate event. If input is in "hh:mm:ss" format, then only the CmdStartButton will be enabled to permit user to click it.

    __________________________________End__________________________________

Post a reply

Enter your message below

Sign in or Join us (it's free).