Validate TextBox Contents Before Use

Antrac1t

Member
Joined
Apr 28, 2014
Messages
13
Programming Experience
Beginner
Thanks, its help, but now i have little bit different issue. I have textbox, where maxlenght = 14 and i need that when this textbox contain string with lenght 14 then data will be saved in database otherwise textbox will be clean. Data will be added by scanner. I tried len function under TextBox1_TextChanged, tried TextBox1.TextLength but nothing is good. I want to avoid timer which will check this textbox every second, dont want any button to execute function. BTW this textbox should contain specific string which i already have in database, i know that this string begins with specific prefix (5 letters), maybe this can be used.

Thank you guys

BR

Antrac1t
 
This question has absolutely nothing to do with the topic of the thread it was posted in so I have moved it to its own thread. Please keep each thread to a single topic and each topic to a single thread. Doing so makes the forums easier to use for you asking the questions, for us answering the questions and anyone who is searching for information later on.
 
You're going to have to be a bit more specific about what you want to happen. Firstly, you say that the TextBox has its MaxLength property set so we know that the TextLength can never be more than that value. Are you saying that you want to exclude values that are shorter than that maximum length? Also, you say that you don't want a Button to have to execute the function but presumably the actual saving of data has to be initiated by a clicking a Button or the like.
I tried len function under TextBox1_TextChanged, tried TextBox1.TextLength but nothing is good.
Then you did it wrong. Maybe if you were to show us what you think is correct then we could tell why it's not. If you want to validate the data when it's entered then there's really only one good way:
VB.NET:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    If TextBox1.TextLength <> TextBox1.MaxLength Then
        TextBox1.Clear()
    End If
End Sub
The issue there is obviously that the text would have to be entered in one go or else the control would be continually cleared. If that's an issue then the obvious option is to handle the Validating event, which exists specifically for performing validation. If validation fails then you set e.Cancel to True and the control will not lose focus.
 
Dear jmcilhinney,
firstly i am sorry that i continued in old thread.
Now about textbox problem: i need textbox where user can scan data (user is able to scan data with lenght 5 to 23 (big paper with XX barcodes), but i am interested in only one type - 14 letter string). When user will scan my datatype then it will be saved in database and will show some work instruction (this i already have). In my prototype i do it with len function and every 2s timer reseted the textbox (it's totally dirty code) and i am not able to figure out how to fix it. If i could use button then it will be easy, but requirement is that user should not use mouse/touchpad only keep in hand scanner :D. Plus one notice, scanner not working that he will put all string in one time, but its character by character

Thank you for your time and ideas

BR

Antrac1t
 
I would suggest that you remove the MaxLength limitation and just let the user enter whatever they want. You can then handle the Validating event, as I've already suggested, and validate the data there. You can test the length and content however you like and cancel the event if it fails.
 
yes, but when i will write some character into textbox1 then it will be cleared, because lenght is less then 14
VB.NET:
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.TextLength = 14 Then
        Else
            TextBox1.Clear()
        End If
    End Sub
 
Last edited:
yes, but when i will write some character into textbox1 then it will be cleared, because lenght is less then 14
VB.NET:
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.TextLength = 14 Then
        Else
            TextBox1.Clear()
        End If
    End Sub

You've ignored what I suggested so I'm not sure what you expect me to say. If the code you have doesn't work then maybe you should consider doing what I told you to do.
 
Back
Top