0

can someone help me to make regex for me?

  • only numbers (positive and negative)
  • only max one dot (optional)
  • max precision 0.00 (optional)
  • comma (,) should be replaced with dot (.)

Here is the code:

<TextBox PreviewTextInput="NumberValidationTextBox"/>

private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9.]+");
            e.Handled = regex.IsMatch(e.Text);
        }

Thanks.

online123
  • 66
  • 9
  • WHat do you mean by "comma (,) should be replaced with dot (.)"? Do you need match only numbers with dot, or do you want to match both, and if comma present - replace it with dot? – markalex Apr 02 '23 at 16:30
  • If comma is pressed should be replaced with dot. Match examples: 123 / 12.2 / 123.23 / 0.23 / -123 / .12 / etc. – online123 Apr 02 '23 at 16:32
  • Do not use a TextBox for that. Use a numeric input component. _Much_ less hassle. – Fildor Apr 02 '23 at 17:04
  • e.Text gives the last character typed and not the content of TextBox. See my solution. – Frenchy Apr 03 '23 at 09:23

2 Answers2

2
“^-?[0-9]*[\\.,]?[0-9]?[0-9]?$”

This detects valid input but does not transform the ‘,’ to a ‘.’. Just follow up with a string replace to handle that. You will be able to safely assume that any ‘,’ in the test at this point should be ‘.’.

  • In regex101.com works great but not in my code. In my code is blocking the numbers, dots. When change code to this "e.Handled = !regex.IsMatch(e.Text);" then it allows more dots. Any suggestions? – online123 Apr 02 '23 at 19:02
  • I suggest a combination of "TryParse" and some manipulation of the entered code. It is also important to think about the echo logic. The echo logic is what we present in the Text while the user is typing and the code accept/reject/replace what is typed. You can have a look at another solution I suggested. https://stackoverflow.com/questions/75460472/hi-i-want-validate-textbox-wpf-mvvm-pattern-to-allow-number-with-decimal-value/75464023#75464023 – Gilad Waisel Apr 02 '23 at 19:50
1

The problem is in your method:

e.Text gives the character typed and not the content of TextBox

so this solution gives the result waited:

    private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
    {
        Regex regex = new Regex("^-?[0-9]*[\\.,]?[0-9]?[0-9]?$");
        var futureText = $"{(sender as TextBox).Text}{e.Text}";
        e.Handled = !regex.IsMatch(futureText);
    }

To replace , by . i suggest you to add the TextChanged event:

    private void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        var myInput = sender as TextBox;

        myInput.Text = myInput.Text.Replace(",", ".").Trim();
        myInput.CaretIndex = myInput.Text.Length;
    }

   <TextBox PreviewTextInput="NumberValidationTextBox" TextChanged="OnTextChanged"/>

i play with the property CaretIndex to keep the Cursor in last position (Replace resets the position of Cursor in first position)

And i have added .trim() to myInput.Text = myInput.Text.Replace(",", ".").Trim(); because previewtextInput event doesnt play with space (its normal)

Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • It's possible to use the method's in other xaml files (no cope/paste)? – online123 Apr 03 '23 at 18:19
  • what do you mean by: use method in other xaml? – Frenchy Apr 03 '23 at 18:24
  • I have many .xaml files. I want to create separate .cs file with NumberValidationTextBox, OnTextChanged methods, and use the methods in all my .xaml files. – online123 Apr 03 '23 at 18:26
  • for example create textBox as usercontrol and add it in each view you need. so you 'll just have one code for all view with same usercontrol. if you dont know haow to do that open a new question and i'll see – Frenchy Apr 03 '23 at 18:47
  • Here the question: https://stackoverflow.com/questions/75932472/how-to-use-same-method-in-more-xaml-files-in-textbox-textchanged-ontextchanged – online123 Apr 04 '23 at 17:58
  • I found problem with the code: when the typed number is "full" example: `123.45` then after highlight the number it's not possible to type other number. I need to use backspace before type new number. Can u help me? – online123 Apr 07 '23 at 18:01