stopping people from entering non numeric values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tolkienarda
    Contributor
    • Dec 2006
    • 316

    stopping people from entering non numeric values

    hi all
    i am trying to stop people from entering anything but numbers into a textbox.
    i have the code to make sure they have entered a number on each keypress event and now a msgbox appears if they enter a non numeric value but how do i stop the character from appearing in the textbox.

    here is my code
    Code:
        Private Sub txtA_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtA.KeyPress, txtB.KeyPress, txtC.KeyPress
            Dim key As Char
            Dim intkey As Integer
            key = e.KeyChar
            intkey = Asc(key)
            If (intkey >= 48 And intkey <= 57 Or intkey = 8 Or intkey = 9 Or intkey = 13) Then
                Exit Sub
            Else
    'here i would like somthing that stops the non numeric value from appearing in the textbox
                MsgBox("only numbers please")
            End If
    
    
        End Sub
    thanks eirc
  • Ali Rizwan
    Banned
    Contributor
    • Aug 2007
    • 931

    #2
    Originally posted by tolkienarda
    hi all
    i am trying to stop people from entering anything but numbers into a textbox.
    i have the code to make sure they have entered a number on each keypress event and now a msgbox appears if they enter a non numeric value but how do i stop the character from appearing in the textbox.

    here is my code
    Code:
        Private Sub txtA_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtA.KeyPress, txtB.KeyPress, txtC.KeyPress
            Dim key As Char
            Dim intkey As Integer
            key = e.KeyChar
            intkey = Asc(key)
            If (intkey >= 48 And intkey <= 57 Or intkey = 8 Or intkey = 9 Or intkey = 13) Then
                Exit Sub
            Else
    'here i would like somthing that stops the non numeric value from appearing in the textbox
                MsgBox("only numbers please")
            End If
    
    
        End Sub
    thanks eirc
    I think you are using VB2005
    I only know VB6 but a bit about 2005
    this code will help you in vb2005 too and in vb5 and vb6 too

    If keyAscii <> 8 Then
    If Instr("01234567 89", Chr(KeyAscii)) = 0 Then KeyAscii = 0
    End If

    you can also allow ascii 13
    GOODLUCK

    Comment

    • tolkienarda
      Contributor
      • Dec 2006
      • 316

      #3
      Originally posted by Ali Rizwan
      I think you are using VB2005
      I only know VB6 but a bit about 2005
      this code will help you in vb2005 too and in vb5 and vb6 too

      If keyAscii <> 8 Then
      If Instr("01234567 89", Chr(KeyAscii)) = 0 Then KeyAscii = 0
      End If

      you can also allow ascii 13
      GOODLUCK
      hi Ali

      yes i am using VB2005, sorry i didn't clarify,
      KeyAscii is not Declared i have seen other scripts where it was used but they were all VB v6 the closest thing in VB 2005 is the e variable, but i can't figure out how to use that to change or delete a value in a textbox.
      if you have any ideas i would be grateful

      eric

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Ok, I'm VB6-only so this may not help with your version. But in VB6, you cannot (AFAIK) cancel a key in the KeyPress event. You have to do it in the KeyDown event.

        Comment

        • Ali Rizwan
          Banned
          Contributor
          • Aug 2007
          • 931

          #5
          Originally posted by tolkienarda
          hi Ali

          yes i am using VB2005, sorry i didn't clarify,
          KeyAscii is not Declared i have seen other scripts where it was used but they were all VB v6 the closest thing in VB 2005 is the e variable, but i can't figure out how to use that to change or delete a value in a textbox.
          if you have any ideas i would be grateful

          eric
          Ok
          write this code in keypress event of textbox
          and add following code

          If KeyAscii > 64 Then
          If KeyAscii < 122 Then
          Else
          MsgBox "Integers Not allowed"
          Text1.Text = ""
          End If
          Else
          MsgBox "Integers Not allowed"
          Text1.Text = ""
          End If

          It will work
          GOOD LUCK

          Comment

          • Ali Rizwan
            Banned
            Contributor
            • Aug 2007
            • 931

            #6
            Originally posted by tolkienarda
            hi all
            i am trying to stop people from entering anything but numbers into a textbox.
            i have the code to make sure they have entered a number on each keypress event and now a msgbox appears if they enter a non numeric value but how do i stop the character from appearing in the textbox.

            here is my code
            Code:
                Private Sub txtA_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtA.KeyPress, txtB.KeyPress, txtC.KeyPress
                    Dim key As Char
                    Dim intkey As Integer
                    key = e.KeyChar
                    intkey = Asc(key)
                    If (intkey >= 48 And intkey <= 57 Or intkey = 8 Or intkey = 9 Or intkey = 13) Then
                        Exit Sub
                    Else
            'here i would like somthing that stops the non numeric value from appearing in the textbox
                        MsgBox("only numbers please")
                    End If
            
            
                End Sub
            thanks eirc
            Hey
            I found a thing uses in vb2005
            it is

            Private Sub TextBox1_KeyPre ss(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles TextBox1.KeyPre ss
            If IsNumeric(e.Key Char) Then
            e.Handled = False ' I dont know about this
            Else
            e.Handled = True ' I dont know about this
            End If
            End Sub

            but this is also made for putting only integers in textfield
            GOODLUCK

            Comment

            Working...