1

I need to validate some textbox input. I want to use a regex.

The question:

Where do I test my regex against the textbox input?

By using the KeyPress event I can only access the old text of the textbox. I cant access the text with the new input included. How do I include the textbox text with the new input? I could just do text = text + input, but that ignores the fact that the input could be in the middle of the text, not always at the end. Any ideas?


Below is the text of the function in case it is useful.

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim decimalRex As Regex = New Regex("^[0-9]*[.]{0,1}[0-9]*$")'checks for decimal'

    If decimalRex.IsMatch(sender.Text) Then'notice this only tests the text, not the input'
        e.Handled = False
    Else
        e.Handled = True
    End If
 End Sub

EDIT:

I ended up not including one of the requirements I needed. I wanted to be able to sanitize the input to the textbox on the fly, and making sure the bad input never appears in the textbox.
I accepted the best answer to my question, even though that is what I needed. The solution to what I needed is below:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If IsNumeric(e.KeyChar) Then
        e.Handled = False
    ElseIf e.KeyChar = "." Then
        If InStr(sender.text, ".") > 0 Then
            e.Handled = True
        Else
            e.Handled = False
        End If
    ElseIf Asc(e.KeyChar) = 8 Then
        e.Handled = False
    Else
        e.Handled = True
    End If
End Sub
1
  • You should use the RegularExpressionValidator. Commented Dec 6, 2010 at 15:45

3 Answers 3

1

Use the following to capture non-matching characters for each key, and this method could easily be modified to do what you're asking... however see below.

Private Sub ONSTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    If e.KeyChar = Microsoft.VisualBasic.Strings.ChrW(Keys.Back) Then
        Exit Sub
    End If

    If Not characterRegex.IsMatch(e.KeyChar.ToString()) Then
        e.Handled = True
    End If
End Sub

If you're attempting to validate the full string, you really should be using the Validating event, and do a regex of the full string there.

Private Sub ONSTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
    If Not validationRegex.IsMatch(Text) Then
        errorProvider.SetError(Me, "Error Message Here")
        e.Cancel = True ' Keeps them from changing to another control until error is corrected.
    End If
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Accepted as the best answer to my original question, though not the best answer to what I needed.
1

You will want to do this in the EditValueChanged Event of the TextBox

Comments

1

You could use the TextChanged event and validate your text there (if you don't use something fancier like bindings on your text).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.