Question Text box validate

ahhak1989

Member
Joined
Sep 18, 2009
Messages
5
Programming Experience
Beginner
I have a question about how to validate a text box with only word inside. Below is my code i use, it's only can detect if number 1 to 9 in single number, but not 11 or 999 or Joanne11 Joa223nne etc. I want it can detect the text box have and number or symbol then will show an error messageBox, else continue.

Please help me, I think for it for few days already.
Thank You.

VB.NET:
Public Class Task2
    Dim userName As String

Private Sub btnContinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinue.Click
        userName = txtName.Text

        If userName Like "[0-9]" Then
            MessageBox.Show("Error", "Invalid Input!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            MessageBox.Show("Hello " & userName & vbNewLine & "You have chosen the " & colorName & " scheme.", "Greeting", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub
End Class
 
Enteer the following code in the Textbox's Keypress event. It will only allow letter keys to be entered by the user, and will also allow the backspace and Enter keys to work:


VB.NET:
   Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsLetter(e.KeyChar) Then e.Handled = True 'letters only
        If e.KeyChar = Chr(8) Or e.KeyChar = Chr(13) Then e.Handled = False
    End Sub
 
Thanks, it's work...
But if want to detect the user input is having number in the text box in any location within the textBox and display an error messageBox, how to do that? Is that possible?
 
Enteer the following code in the Textbox's Keypress event. It will only allow letter keys to be entered by the user, and will also allow the backspace and Enter keys to work:


VB.NET:
   Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsLetter(e.KeyChar) Then e.Handled = True 'letters only
        If e.KeyChar = Chr(8) Or e.KeyChar = Chr(13) Then e.Handled = False
    End Sub

Look at this code carefully, where does he check if the input is not a letter?, then put the error message there. You must learn to be able to apply logical thinking when programming, it will be of great help in the future.
 
Back
Top