Results 1 to 10 of 10

Thread: Validating text entered into a VB text box

  1. #1
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)

    Validating text entered into a VB text box

    Ok im makeing a program which asks a question and the user has to enter an answer into a txt box. When they do this an OK button appears and when that is clicked it tells them if their question is right or wrong. The problem i have is answers can only be numbers and not letters. How do i get the "ok" button to validate the answer in the text box. If a number is entered the program just gives feed back to the answer . If any thing other than a number is entered it displays a message saying "only numbers can be entered" for example. How do i do this? thanks for anyhelp.

    Edit: I know how to do all the other things mentioned. Its just that validation thing i cant work out

  2. #2
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,773
    Thanks
    22
    Thanked
    40 times in 24 posts
    If not isNumeric(val) then...
    Simon


  3. #3
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    In this situation it is better to prevent the user from entering letters in the text box rather than wait til they press the OK button.

    What you need to do is handle the KeyPress event and check which key was pressed. If you set the keycode to 0 they the KeyPress is effectively cancelled.

    In the code below all key presses are cancelled apart from 0 to 9...

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case Asc("0") To Asc("9")
                ' Do nothing
            Case Else
                KeyAscii = 0
        End Select
    End Sub

  4. #4
    Senior Member
    Join Date
    Jul 2003
    Location
    ZA ✈ UK
    Posts
    622
    Thanks
    0
    Thanked
    0 times in 0 posts
    Note that Mart's method will not work if the user pastes text using the right-click pop-up menu. Note also that Mart's method prevents the user from pressing backspace (ASCII char 8, I think).

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case Asc("0") To Asc("9"), 8 'Allow backspace
                ' Do nothing
            Case Else
                KeyAscii = 0
        End Select
    End Sub
    
    Private Sub Text1_Change()
        Dim i as Long, sTemp as String
    
        For i = 1 To Len(Text1)
            If Instr(1, "0123456789", Mid$(Text1, i, 1)) > 0 Then sTemp = sTemp & Mid$(Text1, i, 1)
        Next
    
        Text1 = sTemp
    End Sub
    That ought to do it. Mind you, I don't currently have VB installed, so the counting might be off, in which case, change "For i = 1" to "For i = 0". Hopefully, though, it's right. Stupid 1-based counting.

  5. #5
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    Note Mart is a VC++ programmer not a VB programmer

  6. #6
    Senior Member
    Join Date
    Jul 2003
    Location
    ZA ✈ UK
    Posts
    622
    Thanks
    0
    Thanked
    0 times in 0 posts
    Fairly nice code you got there for a non-VB programmer.

    Myself, I'm currently employed coding VB. Though I know several other languages, a smidgeon of VC++ too.

  7. #7
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)
    Nice one. thanks everyone

  8. #8
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)
    Re opening an old post

    as i said above thanks for all your help. the code eldren provided worked best for my situation however i now need prevent the user from entering zero first then enter numbers after it e.g. 0333. However they are allowed to enter zeros after a first number is entered whcih is not zero e.g. 10350. How is this done? took 0 away from the list of allowed character but realized then that no 0 could be entered which is where i got stuck. lol any ideas?

  9. #9
    Senior Member
    Join Date
    Jul 2003
    Location
    ZA ✈ UK
    Posts
    622
    Thanks
    0
    Thanked
    0 times in 0 posts
    First, declare a module-level variable that will tell us whether there is a number in the textbox or not. For example: Private blnAllowZero As Boolean

    Now, in the Keypress sub, change 'Asc("0") To Asc("9")' to 'Asc("1") To Asc("9")'. Next, in the same Select block, add a case for zero itself, 'Case Asc("0")'. Inside that, put: If blnAllowZero = False Then KeyAscii = 0 'Block 0 if not allowed

    Lastly, in the Change sub, add the following at the end, just before End Sub: blnAllowZero = (Len(Text1) > 0)


    Hope you come right. :>

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    1
    Thanks
    0
    Thanked
    0 times in 0 posts

    Would this work

    You could try this it basicly makes your textbox an interger
    Code:
    Private Sub Button_Click()
    
        Dim number As Integer
            'here the varible number is dimed as a interger (number)
            
        On Error Resume Next
            'if it errors which it will if anything but a number is entered_
             resume next
            
        number = Text1.Text
            'text is number which is a interger numbers only
            
        If Err <> 0 Then MsgBox ("number only") Else  
            'if it errors then message else do what you want it to do
    End Sub
    Last edited by hell_flame85; 09-06-2006 at 10:17 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •