Results 1 to 4 of 4

Thread: Text Box to allow only letters & numbers

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2000
    Location
    Newcastle-Under-Lyme,Staffs,England
    Posts
    1

    Post

    I want a text box to only accept letters & numbers. I dont want to use a masked edit box as this limits the length of the text to be entered. I assume I have to use Win32 api setwindowlong, etc. Can anyone help?

  2. #2
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    Calgary Alberta
    Posts
    359

    Post

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii >= 48 And KeyAscii <= 57 Then
        'this means it's a number
    ElseIf KeyAscii >= 65 And KeyAscii <= 90 Then
        'this means it's a capital letter
    ElseIf KeyAscii >= 97 And KeyAscii <= 122 Then
        'this means it's a lower case letter
    Else
        KeyAscii = 0    'it won't write anything to the text box.
        'this means it's a different character
    End If
    End Sub
    in the ELSE where I set KeyAscii = 0 you could put a message box telling them they entered an invalid character.

  3. #3
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    455

    Post

    Hello DHorton,

    Hopefully this will help you.

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    'character Ascii
    '0 - 9 48 - 57
    'A - Z 65 - 90
    'a - z 97 - 122


    If Not (KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii >= 65 And KeyAscii <= 90 Or KeyAscii >= 97 And KeyAscii <= 122) Then KeyAscii = 0
    End Sub


    Nice regards,

    Michelle.

  4. #4
    Lively Member
    Join Date
    Mar 2000
    Posts
    82

    Post

    or...

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    Case Is <= 32, 48 To 57, 65 To 90, 97 To 122
    KeyAscii = KeyAscii
    Case Else
    KeyAscii = 0
    End Select
    End Sub

    the is <= 32 is to let the user use the control keys
    backspace, delete, etc....

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