Results 1 to 3 of 3

Thread: Validating user input

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Posts
    3
    I've written a small code that converts binary numbers into hex and decimal numbers. I want to restricting user input to just 0's and 1's but I'm having a bit of trouble.

    Please can you help me.

    Sarah Jamerson

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Put this into the keypress event of the control.

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
      'if the key pressed is not 1 or 0 then set keyascii = 0.
      If (keyascii <> 48)  And (keyascii <> 49) then
        KeyAscii = 0
      End If
    End Sub
    This locks out all keys except the 0 or 1 key. You might want to let them press the backspace key. If so then add a condition of "keyascii <> 8".

    Iain.

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    India
    Posts
    298
    u can use the following code in the keypress event of ur textbox:

    -----------------------------

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not (KeyAscii = 48 Or KeyAscii = 49 Or KeyAscii = 8) Then
    KeyAscii = 0
    End If
    End Sub

    -----------------------------

    48 --> ascii vslue of zero
    49 --> ascii vslue of one
    48 --> ascii vslue of backspace

    This should work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width