1

I have a code

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

I need input number or backspace.How i can disable input symbol space?

  • Ty to append '$' at the end (^[0-9]*$) Check this answer http://stackoverflow.com/questions/19715303/regex-that-accepts-only-numbers-0-9-and-no-characters – Mohammed Alasa'ad Oct 06 '15 at 08:59

1 Answers1

1

The C-style escape for the backspace character is \b which is also the Regex escape for a word boundary. Fortunately you can put it in a character class to change the meaning:

e.Handled = Regex.IsMatch(e.Text, "[0-9\b]+");
Corey
  • 15,524
  • 2
  • 35
  • 68
  • 1
    Your code does not fit, I need to be able to enter only numbers and backspace –  Oct 06 '15 at 09:24
  • So flip the condition. The character range `[0-9\b]` is all digits and backspace. If you want to detect anything else you flip it to `[^0-9\b]` - anything except numbers and backspace. – Corey Oct 06 '15 at 23:10
  • [^0-9\b] it is good version, but still entered the space character.How i can input ban space character? –  Oct 07 '15 at 05:21
  • That pattern does not match the space character, only the backspace and numerals. How about you edit your question with some more code and explanation of the way you're using it so I have a better idea of what you're actually doing. – Corey Oct 07 '15 at 10:35