how to validate string input in textbox

newstu

New member
Joined
Jan 8, 2014
Messages
3
Programming Experience
Beginner
How to restrict any input except letters and spaces in text box, if I need to run code for replacing any word with any word in phrase.
        Dim phrase As String
        Dim word1 As String
        Dim word2 As String
 
       
       
       phrase = txtphrase.Text
        word1 = txtword1.Text
        word2 = txtword2.Text


thanks
 
Last edited by a moderator:
Firstly, you have to specify a syntax option when using xcode tags so I have fixed that for you.

As for the question, just to confirm, are you saying that you only want to allow letters and spaces in TextBox? Do you want to prevent the user entering invalid characters as they type or do you want to validate the text afterwards?
 
I know there is option to validate input of numbers only(like If Not IsNumeric), is the re anything similar for string?
 
Replace invalid characters.

You can prevent incorrect entries directly in the textbox using the KeyPress event, as shown below.

To validate after entries are completed, use a variation of the following code which will remove the incorrect character from the textbox and inform the user each time. The code replaces the character with nothing but you can replace it with a ? or * or whatever. Otherwise you can simply inform the user that the input was incorrect without removing the character, or you could just clear the textbox and have the user reenter the correct string.

VB.NET:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        For Each ch As Char In TextBox1.Text
            If Not (Char.IsLetter(ch) Or ch = " ") Then
                TextBox1.Text = TextBox1.Text.Replace(ch, "")
                MessageBox.Show("Incorrect character removed.", ch)
            End If
        Next ch
    End Sub

    Private Sub TextBox2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        If Not (Char.IsLetter(e.KeyChar) Or e.KeyChar = " ") Then e.Handled = True
    End Sub

Note: There is no method for testing the entire string for all-letter characters. The Char class methods test only for single characters one at a time, which is why I placed it inside a loop. If you don't want the messagebox popping up after each wrong character, then just flag it with a Boolean variable and don't remove the character. Notify the user to reenter the string at the end of the loop.
 
Last edited:
As Solitaire says, there's no way to test an entire String in one go for invalid characters but you can use LINQ to collapse a loop down so it pretty much seems that you can, e.g.
Private Sub TextBox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    If Not Me.TextBox1.Text.All(Function(ch) Char.IsLetter(ch) OrElse ch = " "c) Then
        MessageBox.Show("Please enter only letters or spaces.")

        'Don't let the user navigate away from the TextBox while it contains invalid characters.
        e.Cancel = True
    End If
End Sub
 
Last edited:
Back
Top