How to validate the textbox?If empty return error message
Printable View
How to validate the textbox?If empty return error message
VB Code:
If TextboxName.text = "" Then MessageBox.Show("Please enter something") End If
Code:
if textbox1.text="" then
msgbox("invalid")
end if
it's a good idea to put the focus back on the textbox, and also you need those code in the Validated event of your textbox. Use the Trim function to remove empty space also (so if they enter " " it would be considered "")
VB Code:
Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated If TextBox1.Text.Trim = "" Then TextBox1.Focus() MessageBox.Show("Boo!") End If End Sub
If want to validate more than 1 text box can i include inside the sub procedure?
Just an addition to the not polite guy. :bigyello:VB Code:
Private Sub txt_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles [u]TextBox1.Validated, TextBox2.Validated, TextBox3.Validated[/u] Dim t As TextBox = CType(sender, TextBox) If t.Text = "" Then t.Focus() MessageBox.Show("Boo!") End If End Sub
Hi,
I didn't understand this part
Dim t As TextBox = CType(sender, TextBox)
Thank You
sender object (the one who sends a message to the event) is casted to it's TextBox form. Then pass the instance to t which of type TextBox. Then we can manipulate t, ie. display the TextBox's Name property or Tag, etc.
Hi,
Private Sub txt_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated, TextBox2.Validated, TextBox3.Validated
Dim t As TextBox = CType(sender, TextBox)
If t.Text = "" Then
t.Focus()
MessageBox.Show("Boo!")
End If
End Sub
This code just validates only the first field
Hi,
Private Sub txt_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated, TextBox2.Validated, TextBox3.Validated
Dim t As TextBox = CType(sender, TextBox)
If t.Text = "" Then
t.Focus()
MessageBox.Show("Boo!")
End If
End Sub
This code just validates only the first field
Hi,
The it depends on what you want to validate. If you want to check each keypress for a valid number see the codebank
http://www.vbforums.com/showthread.php?t=314936
If you want to check for something other than numerics you should be able to adapt that code.