none
Custom TextBox Regex not worling RRS feed

  • Question

  • I am building a custom TextBox that I want to limit the decimal places to 2. This code below works fine for numeric and decimals, but doesn't limit the number of decimal places to 2.

        Protected Overrides Sub OnPreviewTextInput(e As System.Windows.Input.TextCompositionEventArgs)
            Dim Pattern As String Strin.Empty

            Pattern = "^(?:\d+\.?\d*|\d+\.?\d+)[\r\n]*$"

            Dim _regex As New Regex(Pattern)
            e.Handled = Not _regex.IsMatch(Text.Insert(SelectionStart, e.Text))

            MyBase.OnPreviewTextInput(e)
        End Sub

    When I change the regex to this:

        Protected Overrides Sub OnPreviewTextInput(e As System.Windows.Input.TextCompositionEventArgs)
            Dim Pattern As String Strin.Empty

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

            Dim _regex As New Regex(Pattern)
            e.Handled = Not _regex.IsMatch(Text.Insert(SelectionStart, e.Text))

            MyBase.OnPreviewTextInput(e)
        End Sub

    The regex fails as soon as I put the . in. If I test this regex on a normal TextBox using _regex.IsMatch(MyTextBox.Text), the regex works fine. I can't see what's wrong.

    Thursday, May 9, 2013 11:38 AM

Answers

  • I suspect that you're trying to use the regex to allow or deny keystrokes.  In that case be aware that you cannot use the RE if it has any grouping requirements.  In the RE I posted "45" is allowed but as soon as the user enters "45." it fails because the dot requires digits after it.  If the user were to paste "45.6" then it would have been permitted.

    You need to adjust the RE if you want to do per-character validation but I'm going to caution you that these types of controls are generally frustrating to right and even more frustrating to use.  There are also quite a few free libraries that already provide this functionality if you don't want to write it yourself.

    Here's the updated RE to allow the above mentioned text entries (and to fix a bug that was in the original)

    ^\d+\.?\d{0,2}$

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Friday, May 10, 2013 1:07 PM
    Moderator

All replies

  • How about this one?

    ^\d+(.\d{0,2})?$

    It allows one or more digits followed by an optional decimal and 0 to 2 digits.  I use Regex Designer to test my regexs and it properly matches (or not) the values you mentioned.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, May 9, 2013 2:08 PM
    Moderator
  • Thanks CoolDadTx,

    It's not the Regex that's failing by the looks of things, it's where

     e.Handled = Not _regex.IsMatch(Text.Insert(SelectionStart, e.Text))

    is executed. Because of the decimal point I'm typing, it can't handle it.

    Friday, May 10, 2013 8:28 AM
  • I suspect that you're trying to use the regex to allow or deny keystrokes.  In that case be aware that you cannot use the RE if it has any grouping requirements.  In the RE I posted "45" is allowed but as soon as the user enters "45." it fails because the dot requires digits after it.  If the user were to paste "45.6" then it would have been permitted.

    You need to adjust the RE if you want to do per-character validation but I'm going to caution you that these types of controls are generally frustrating to right and even more frustrating to use.  There are also quite a few free libraries that already provide this functionality if you don't want to write it yourself.

    Here's the updated RE to allow the above mentioned text entries (and to fix a bug that was in the original)

    ^\d+\.?\d{0,2}$

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Friday, May 10, 2013 1:07 PM
    Moderator
  • Thanks - that worked a treat.
    Friday, May 10, 2013 3:20 PM