Results 1 to 19 of 19

Thread: Disable alphabet and special characters

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    87

    Disable alphabet and special characters

    how will i do that?
    I Hope I Could be Great Like You

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Disable alphabet and special characters

    Where?
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Disable alphabet and special characters

    Are you talking about only allowing numeric input into something like a text box?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    87

    Re: Disable alphabet and special characters

    yup,,
    I Hope I Could be Great Like You

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Disable alphabet and special characters

    There are a number of ways of accomplishing this, but I think the easiest is to use the Textbox control that Martin Liss created which, by its nature, only allowes numbers and has a few other very cool features.

    Check this out.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    87

    Re: Disable alphabet and special characters

    wat if i want to code only the textbox?!?
    I Hope I Could be Great Like You

  7. #7
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Disable alphabet and special characters

    This one?
    VB Code:
    1. Option Explicit
    2. Private Sub Text1_KeyPress(KeyAscii As Integer)
    3.     'Check wheather number is entered
    4.     If IsNumeric(KeyAscii) = True Then
    5.      'if yes then allow the entry
    6.      KeyAscii = KeyAscii
    7.     Else
    8.     'dont allow the entry
    9.     KeyAscii = 0
    10.     End If
    11. End Sub
    Please mark you thread resolved using the Thread Tools as shown

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Disable alphabet and special characters

    Thread starter, keep in mind that most solutions perform the check during keyboard input. In the end, you will still have to recheck the final input in order to handle inputs through copy+paste.

  9. #9
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Disable alphabet and special characters

    As per Leinad advice,
    I have added this one also
    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.     If IsNumeric(Text1.Text) = True Then
    3.         Cancel = False
    4.     Else
    5.         MsgBox "Not a Valid Numeric Entry"
    6.         Cancel = True
    7.     End If
    8. End Sub
    Please mark you thread resolved using the Thread Tools as shown

  10. #10
    New Member PrattDiddy's Avatar
    Join Date
    Feb 2011
    Posts
    1

    Re: Disable alphabet and special characters

    I tried using this but when I stepped through and got to IsNumeric(KeyAscii), it always came out to be true even when using letters. I'm trying to only allow numbers in a text box in VB6. Any ideas on accomplishing this?

  11. #11
    Addicted Member
    Join Date
    Aug 2005
    Posts
    222

    Re: Disable alphabet and special characters

    Hi;

    Wellcome to forum

    This new code sample which belong to Martin Liss
    Code:
    Private Sub Textbox_KeyPress(KeyAscii As Integer)
    
        ' Allow only numbers and delete
        Select Case KeyAscii
            Case 48 To 57, 8
                'okay - do nothing
            Case Else
                ' 'Eat' the input
             KeyAscii = 0
        End Select
    
    End Sub
    Last edited by levent; Jul 16th, 2011 at 10:26 AM.

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Disable alphabet and special characters

    Quote Originally Posted by jeric_mandrake View Post
    how will i do that?
    Quote Originally Posted by PrattDiddy View Post
    I tried using this but when I stepped through and got to IsNumeric(KeyAscii), it always came out to be true even when using letters. I'm trying to only allow numbers in a text box in VB6. Any ideas on accomplishing this?
    When you compile the code in this project it produces my NumberBox ActiveX control that is a textbox that allows only numbers. It also has these additional properties:
    CanBeNegative - True/false
    CanHaveDecimals - True/False
    DecimalSeparator - period/comma
    MaxDecimals - maximum number of decimals allowed
    MaxValue - highest value allowed
    MinValue - smallest number allowed
    RequireLeadingDigit - True/False

  13. #13
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Disable alphabet and special characters

    jeric, u can simply restrict to type only numbers including negative sign, decimal sign, backspace etc.. to the textbox with keypress event as u got examples above, but remember to block the right click function also, since user can paste letters to the textbox.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  14. #14
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Disable alphabet and special characters

    Quote Originally Posted by seenu_1st View Post
    jeric, u can simply restrict to type only numbers including negative sign, decimal sign, backspace etc.. to the textbox with keypress event as u got examples above, but remember to block the right click function also, since user can paste letters to the textbox.
    The trouble with the "simple" code in some of these posts is that it's too simple. Consider the following

    You can't paste 123 or any valid number and the user might want to.
    Is -123 a number?
    Is 123- a number?
    Is - 123 a number?
    Is 123.123.123 a number?
    Is 123..123 a number?

  15. #15
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Disable alphabet and special characters

    Martin, i dont say it's simple, i just said simply restrict to type only numbers, but need other contrains to be remembered, i mentioned a point, u mentioned some more thats all, i realy agree with u.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  16. #16
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Disable alphabet and special characters

    try this.. user can input numbers, negative sign, decimal point at right place, check and feed back if any error...
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 45 'negative key
            If Text1.Text = "" Or (Text1.SelStart = 0 And InStr(Text1.Text, "-") = 0) Then
                KeyAscii = 45 ' negative sign
            ElseIf Text1.SelLength = Len(Text1.Text) Then
                KeyAscii = 45 ' negative sign
            Else
                KeyAscii = 0
            End If
        Case 48 To 57, 8  'numbers and backspace
        Case 46 'decimal point
            If Text1.SelLength <> Len(Text1.Text) Then 'not fully selected
                If InStr(Text1.Text, ".") > 0 Then
                    KeyAscii = 0
                End If
            End If
            If InStr(Text1.SelText, ".") > 0 Then 'if point in selected text
                KeyAscii = 46 ' decimal point
            End If
        Case Else
            KeyAscii = 0
    End Select
    
    End Sub
    Last edited by seenu_1st; Jul 16th, 2011 at 11:11 AM.
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  17. #17

  18. #18
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    Re: Disable alphabet and special characters

    Quote Originally Posted by MartinLiss View Post
    All that code and more is in my ActiveX control.
    i think i hav used ur ActiveX control b4 some yrs, is this old one or new one u created?
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  19. #19
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Disable alphabet and special characters

    Quote Originally Posted by seenu_1st View Post
    i think i hav used ur ActiveX control b4 some yrs, is this old one or new one u created?
    This is the only one but it has been upgraded a number of times.

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