Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3481394
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:26:37+00:00 2026-05-18T10:26:37+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:26:38+00:00Added an answer on May 18, 2026 at 10:26 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to validate two user input fields against each other in seam. Field1
I'm coding an italian website where I need to validate some input data with
I m using jquery validator plugin. i need to validate some field with some
I have used jQuery plugin: Validation To validate some input fields, but I need
How can I cancel a form submission using GWT? I need to validate the
Background I need to validate user input in some fields, where these are defining
I have a textbox and in some cases in Enter event I need to
I need to validate some user input that is encoded in UTF-8. Many have
I am trying to use jquery validation to validate some input values on the
I need to validate some user input, to ensure a number entered is in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.