locked
Validating a string in a textbox. RRS feed

  • Question

  • Hi, I am trying to validate a string that has been inputted into a text box to make sure there is nothing but alphabetical charecters in the textbox. The following code only checks the first character in the textbox...

    If Char.IsLetter(textbox1.Text) = False Then
                MsgBox("Error! Invalid charecters present")
                Exit Sub
            End If

    I was wondering if there was any way that i could turn this into some sort of loop so that it checks all of the charecters present within the textbox?

    Wednesday, February 8, 2012 1:01 PM

Answers

  • You can either validate the input as the user types in each character or you can validate it after the user has done typing.

    For the first one, you handle one of the textbox's keyboard events (keypress, keydown or keyup) and test the typed-in character. If it's not what you want, you can show a message and set e.Handled = True so that the bad character is ignored.

    For the 2nd method, you loop thru all the character in the input string and test each one to see if its in a not allowed group of characters. If it is, you exit the loop and show the message to your user. Something like this:

    Dim input as string = textbox1.text

    Dim notAllowedChars as String = "~`!@#$%^&*()_-+={[]}|\;':""<,>.?/1234567890"

    For each c as Char in input

       If notAllowedChars.Contains(c) Then

            MessageBox.Show("Invalid charaters present")

            Exit For

        End If

    Next


    • Edited by Stanav Wednesday, February 8, 2012 1:54 PM
    • Marked as answer by Jerrrum Wednesday, February 8, 2012 1:56 PM
    Wednesday, February 8, 2012 1:53 PM

All replies

  • Hello,

    you can use the like Operator

    regards Ellen


    Ich benutze/ I'm using VB2008 & VB2010

    Wednesday, February 8, 2012 1:34 PM
  • You can either validate the input as the user types in each character or you can validate it after the user has done typing.

    For the first one, you handle one of the textbox's keyboard events (keypress, keydown or keyup) and test the typed-in character. If it's not what you want, you can show a message and set e.Handled = True so that the bad character is ignored.

    For the 2nd method, you loop thru all the character in the input string and test each one to see if its in a not allowed group of characters. If it is, you exit the loop and show the message to your user. Something like this:

    Dim input as string = textbox1.text

    Dim notAllowedChars as String = "~`!@#$%^&*()_-+={[]}|\;':""<,>.?/1234567890"

    For each c as Char in input

       If notAllowedChars.Contains(c) Then

            MessageBox.Show("Invalid charaters present")

            Exit For

        End If

    Next


    • Edited by Stanav Wednesday, February 8, 2012 1:54 PM
    • Marked as answer by Jerrrum Wednesday, February 8, 2012 1:56 PM
    Wednesday, February 8, 2012 1:53 PM
  • The first method works perfectly!! Thanks for the help!
    Wednesday, February 8, 2012 1:56 PM