Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[147] Accept only numbers in a text field

Last post 02-17-2007 5:10 PM by chandra219. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [147] Accept only numbers in a text field

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

    • Post Points: 25
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 04-24-2002 5:44 PM In reply to

    Another Method

    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.
    • Post Points: 0
  • 12-10-2002 3:34 PM In reply to

    A more better way?

    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

    • Post Points: 0
  • 06-09-2005 2:43 PM In reply to

    What I use...

    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)
    • Post Points: 0
  • 02-17-2007 5:10 PM In reply to

    Re: [147] Accept only numbers in a text field

    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 Points: 5
Page 1 of 1 (5 items)