Results 1 to 12 of 12

Thread: [SOLVED]Easy! Restrict textbox to just numbers and letters.

  1. #1

    Thread Starter
    Hyperactive Member squakMix's Avatar
    Join Date
    Oct 2003
    Location
    Washington
    Posts
    342

    [SOLVED]Easy! Restrict textbox to just numbers and letters.

    I know this is an easy one. I have found the answere, but It wasnt quite what i wanted, and it didnt work for me. Can I have some quick help thanks!
    Last edited by squakMix; Oct 22nd, 2003 at 09:39 PM.
    -squaK

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Post the code you think is close, and identify the deficiencies.

    (This question has been answered many, many times before)



    Bruce.

  3. #3

    Thread Starter
    Hyperactive Member squakMix's Avatar
    Join Date
    Oct 2003
    Location
    Washington
    Posts
    342
    This code doesnt work. When i enter a normal character into the textbox, it shows nothing:

    VB Code:
    1. Private Sub Text2_KeyPress(KeyAscii As Integer)
    2.     If Not (KeyAscii > 47 And KeyAscii < 58) Or (KeyAscii > 64 And     KeyAscii < 91) Or (KeyAscii > 96 And KeyAscii < 123) Or (KeyAscii = 95) Then
    3.      KeyAscii = 8
    4.    End If
    I also want to be able to do NON upper case letters also.
    -squaK

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    You wern't evaluating all other NOT's, so try:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Text2_KeyPress(KeyAscii As Integer)
    4.     If Not ((KeyAscii > 47 And KeyAscii < 58) Or (KeyAscii > 64 And KeyAscii < 91) Or (KeyAscii > 96 And KeyAscii < 123) Or (KeyAscii = 95)) Then
    5.         KeyAscii = 0
    6.     End If
    7. End Sub


    Bruce.

  5. #5

    Thread Starter
    Hyperactive Member squakMix's Avatar
    Join Date
    Oct 2003
    Location
    Washington
    Posts
    342
    Thanks. That's what i needed.
    -squaK

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    To clarify: I enclosed the whole 'Or AND' structure (with ()), and if that
    was evaluated, then send KeyAscii = 0.


    Bruce.

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Although that works for the user typing in the TextBox, you should also check the data thats Cut 'n' Pasted!


    One (probably the best) method is to use the Text2_Validate() Event, and if there is an unwanted character, then highlight it and
    set the Validation's Cancel = False (kepping the Focus in the TextBox until its correct)


    Bruce.

  8. #8

    Thread Starter
    Hyperactive Member squakMix's Avatar
    Join Date
    Oct 2003
    Location
    Washington
    Posts
    342
    with the coding you sent me, the user CANT cut and paste! it doesnt allow for ctrl.
    -squaK

  9. #9

    Thread Starter
    Hyperactive Member squakMix's Avatar
    Join Date
    Oct 2003
    Location
    Washington
    Posts
    342
    ...If the user uses the mouse, I have coding that says something like "If len(text2.text) = 0 then"...
    -squaK

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by squakMix
    with the coding you sent me, the user CANT cut and paste! it doesnt allow for ctrl.

    Ahhh, I should have checked that

  11. #11
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by squakMix
    ...If the user uses the mouse, I have coding that says something like "If len(text2.text) = 0 then"...

    Not too sure if you indend that (above) to counter the use of the Mouse|Paste....

    I was able to paste unwanted characters using the Right Mouse Button.

    Again, you may have taken care of that.

  12. #12

    Thread Starter
    Hyperactive Member squakMix's Avatar
    Join Date
    Oct 2003
    Location
    Washington
    Posts
    342
    yea, My actual Coding Has MANY more lines.
    -squaK

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