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

  • SEARCH
  • Home
  • 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 7919835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:59:52+00:00 2026-06-03T15:59:52+00:00

I took many hours trying to solve this problem I have attempted, without success.

  • 0

I took many hours trying to solve this problem I have attempted, without success.
All I need is to validate a textbox:

Valid Chains:

10% 
 0% 
 1111111.12% 
 15.2% 
 10 
 2.3 

Invalid Chains:

 .% 
 12.% 
 .02% 
 % 
 123456789123.123 

I need to validate the textbox with these valid chains, supporting the keypress event.

I tryed:

Private Sub prices_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles wholeprice_input_new_item.KeyPress, dozenprice_input_new_item.KeyPress, _
            detailprice_input_new_item.KeyPress, costprice_input_new_item.KeyPress

            Dim TxtB As TextBox = CType(sender, TextBox)
            Dim fullText As String = TxtB.Text & e.KeyChar
            Dim rex As Regex = New Regex("^[0-9]{1,9}([\.][0-9]{1,2})?[\%]?$ ")

            If (Char.IsDigit(e.KeyChar) Or e.KeyChar.ToString() = "." Or e.KeyChar = CChar(ChrW(Keys.Back))) Then
                If (fullText.Trim() <> "") Then
                    If (rex.IsMatch(fullText) = False And e.KeyChar <> CChar(ChrW(Keys.Back))) Then
                        e.Handled = True
                        MessageBox.Show("You are Not Allowed To Enter More then 2 Decimal!!")
                    End If
                End If
            Else
                e.Handled = True
            End If
        End Sub

NOTE: The regex has to validate (Maximum 2 decimal places, and 9 integers) with an optional percent symbol.

Please help, I feel so frustrated trying to solve the problem without success

  • 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-06-03T15:59:56+00:00Added an answer on June 3, 2026 at 3:59 pm

    I think that you almost had the right answer. When I run your regex against the samples you supplied, they all fail. But if I remove the extra space at the end of the regex I get the expected successes and failures.

    So currently your regex looks like this:

    Dim rex As Regex = New Regex("^[0-9]{1,9}([\.][0-9]{1,2})?[\%]?$ ")
    

    and it should look like

    Dim rex As Regex = New Regex("^[0-9]{1,9}([\.][0-9]{1,2})?[\%]?$")
    

    EDIT:

    Ok I understand the issue more. The problem with the regex is that it will only allow a period if it is followed by one or two numbers. That works fine if you are evaluating the textbox value after someone has finished typing. But in your code, you are evaluating for each keypress, so you don’t have a chance to type a number after the “.”

    I can see two possible solutions

    1. Change the regex to allow 1. as a valid entry
    2. Change when you evaluate the regex, perhaps trying to figure out a way to only evaluate the regex when the person has paused typing.

    If you went with option 1, then we need to tweak the regex to something like this

    "^[0-9]{1,9}((\.)|(\.[0-9]{1,2}(%)?)|(%))?$"
    

    I changed the regex so that it will accept three optional endings to the text string (\.) will allow the string to end in a period , (\.[0-9]{1,2}(%)?) will allow the string to end period followed by one or two numbers and an optional percent sign, and (%) will allow the string to end in a percent sign. I broke the ending into the three options because I didn’t want to allow something like 12.% to be valid. Also for this to work you will also need to add the percent sign to your first If statement

    If (Char.IsDigit(e.KeyChar) Or e.KeyChar.ToString() = "." Or e.KeyChar.ToString() = "%" Or e.KeyChar = CChar(ChrW(Keys.Back))) Then
    

    so that the regex runs when someone types the percent sign.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already looked up many posts about this problem (subqueries being very slow in
I took over an old HTML based site with all hard coded links, no
I took 2 images with a cross point and now I'm trying to compare
I took a look at this answer and it goes in part to solving
Trying to code a guess how many gumballs in the gumballs jar, kinda thing.
I have a folder structure with one main parent folder containing many subfolders, and
This is something that's been on my mind for years, but I never took
I have the following SQL problem. Scenario: I have two tables: Change and ChangeTicket.
there are many related questions/answers about that in SO. I took a look to
I have a very large C project with many separate C files and headers

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.