Results 1 to 10 of 10

Thread: [RESOLVED] Text Box Validation

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    38

    Resolved [RESOLVED] Text Box Validation

    Making a program which uses text boxes to input numbers but at the moment it also allows characters. how can i make it so it only allows numbers i have tryed the data format but it isnt working.

  2. #2
    Addicted Member
    Join Date
    Oct 2006
    Location
    Chennai, India
    Posts
    198

    Re: Text Box Validation

    Make use of the ASC code for the allowable character in the KEYPRESS event of the text box so that only the required characters are allowed.

    eg.

    To Allow letter "A",

    Code:
    Select KeyAscii
    
    Case ASC("A"),.........
    
    Case Else
    
    KeyAscii=0
    
    End Select
    Regards
    Srinivasan Baskaran
    India

  3. #3
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Smile Re: Text Box Validation

    Use this piece of code

    Code:
    Private Sub Text1_Change()
    If Not ValidateNumeric(Text1.Text) Then
        Text1.Text = ""
    End If
    End Sub
    
    Private Function ValidateNumeric(strText As String) _
        As Boolean
    ValidateNumeric = CBool(strText = "" _
        Or strText = "-" _
        Or strText = "-." _
        Or strText = "." _
        Or IsNumeric(strText))
    End Function

  4. #4
    Addicted Member
    Join Date
    Oct 2006
    Location
    Chennai, India
    Posts
    198

    Thumbs up Re: Text Box Validation

    To do a better code for allowing only numbers ,


    Code:
    Select KeyAscii
    
    Case ASC("0") to ASC("9")
    
    Case Else
    
    KeyAscii=0
    
    End Select
    Regards
    Srinivasan Baskaran
    India

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Text Box Validation

    Don't forget to allow for Del, Backspace, (Decimal Point, + and - if necessary) though.

  6. #6
    Addicted Member
    Join Date
    Oct 2006
    Location
    Chennai, India
    Posts
    198

    Re: Text Box Validation

    Yes Doogle, you are right, i forget it in my sample.....
    Regards
    Srinivasan Baskaran
    India

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

    Re: Text Box Validation

    Try this
    vb Code:
    1. 'Place in keypress event of text box to prevent
    2. 'typing in non numeric text
    3. Private Sub Text1_KeyPress(KeyAscii As Integer)
    4. If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 Then
    5.     MsgBox "Only numbers are allowed for this entries"
    6.     KeyAscii = 0
    7.     Exit Sub
    8. End If
    9. End Sub
    10.  
    11. 'also, in the change event put this code
    12. 'to prevent pasting in non numeric text
    13. Private Sub Text1_Change()
    14. If Not IsNumeric(Text1.Text) Then
    15.    MsgBox "Only Numbers Are Allowed"
    16.    Text1.Text = vbNullString
    17.    Exit Sub
    18. End If
    19. End Sub

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Text Box Validation

    Might want to modify that change event some, the Msgbox can pop up twice and you loose everything you already had if you do enter a character.

    Hows This?
    Code:
    Private Sub Text1_Change()
        Static LastGood As String
        
        If Len(Text1) > 0 Then
            If Not IsNumeric(Text1.Text) Then
               MsgBox "Only Numbers Are Allowed"
               Text1.Text = LastGood
               Text1.SelStart = Len(LastGood)
               Exit Sub
            Else
                LastGood = Text1.Text
            End If
        Else
            LastGood = ""
        End If
    End Sub

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    38

    Re: Text Box Validation

    cheers guys very useful

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

    Re: Text Box Validation

    If you have no further questions and you consider this resolved, you could help us out by pulling down the Thread Tools menu and clicking the Mark Thread Resolved menu item. That will let everyone know that you have your answer.

    Thank you.

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