CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 1999
    Location
    Singapore
    Posts
    18

    Input Validation

    Hi, I have create some textboxes and had performed the input validation to block all other keys except from A to Z. However, now I cannot use the backspace key. How can I alter the code so that that key can be used again? The code is as follows:

    Private Sub Textbox1(KeyAscii As Integer)

    Dim StrValid as String

    StrValid = "ABCD....Z"

    If InStr(StrValid, Chr(KeyAscii)) = 0 Then
    KeyAscii = 0
    End If

    End Sub


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Input Validation

    in your KeyPress handler

    If KeyAscii <> 8 then
    If InStr(1, strvalid, Chr(KeyAscii)) = 0 then
    KeyAscii = 0
    End If
    End If






  3. #3
    Join Date
    Oct 1999
    Location
    CA
    Posts
    91

    Re: Input Validation

    The character code for backspace is 8. You could slightly modify your code to:

    strValid = "ABCD....Z" & chr(8)

    If InStr(StrValid, Chr(KeyAscii)) = 0 then
    KeyAscii = 0
    End If




    But I personally would use the following code. I believe it would be faster.

    If Not ((KeyAscii = 8) Or ((KeyAscii >= 65) And (KeyAscii <= 90))) then
    KeyAscii = 0
    End If




    BrewGuru99

    Brewguru99

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

Featured