0

I tried to add regex to TextBox in xaml but after i added it, it doesn't work, can't enter any value. I wanted only value from below to by entered to my textbox. Tried my regex on online test and it works https://regex101.com/r/OtGvt7/1 but it doesnt work in my app.

123-456
23-234
  private void Test1(object sender, TextCompositionEventArgs e)
        {
            if (!Regex.IsMatch((e.Source as TextBox)?.Text + e.Text, "^[0-9]{0,3}(?=-)[0-9-]{0,4}$"))
            {
                e.Handled = true;
            }

     
        }

My second regex works only if i enter 'x' first, it works online https://regex101.com/r/m2ehFK/1

  if (!Regex.IsMatch((e.Source as TextBox)?.Text + e.Text, "^[0-9,][x][0-9,]{0,6}$"))
            {
                e.Handled = true;
            }

but my goal was to achieve something like this

1x21
2,2x1
3,3x1,2
33x1,1
  • Can you try to add "@" to begining of your string ? I mean (!Regex.IsMatch((e.Source as TextBox)?.Text + e.Text, @"^[0-9,][x][0-9,]{0,6}$")) Later, you can check in debug that if UI elements can get error. Sometimes, UI gets error and you cannot see them in debug. I would suggest to activite "All Exceptions" by clicking "ctrl + alt + e" and check if there is any error. – ahmet gül Aug 03 '22 at 14:32
  • What I mean by all exception is explained in here: https://stackoverflow.com/a/116934/8763644 – ahmet gül Aug 03 '22 at 14:35
  • I added "@" and it still doesn't work – Andrzej Dach Aug 04 '22 at 05:15
  • Have you open all unhandled exception and checked in debug ? – ahmet gül Aug 04 '22 at 07:41
  • Yes, no errors is showing – Andrzej Dach Aug 04 '22 at 08:14

0 Answers0