1

I want to make a TextBox as numeric, I tried this function:

   <TextBox Name="txtProductId" PreviewTextInput="Number_Validation"/>


   public static void Number_Validation(object sender,System.Windows.Input.TextCompositionEventArgs e)
   {
       System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[^0-9]+");
       e.Handled = regex.IsMatch(e.Text);
   }

But it accepts 'Space' and number. I don't need the space.

Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35
  • `^[0-9]*$`, try that.. or `[\d]` – Trevor Apr 17 '19 at 17:28
  • when I use `^[0-9]*$` it accept anything letter and number, `[\d]` doesn't work for me – shadow walker Apr 17 '19 at 18:44
  • You can cut and paste samples from other SO answers. EG https://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf – Andy Apr 17 '19 at 18:48
  • Have a look at [this post](https://stackoverflow.com/a/1268648/51882339) it gives you the opportunity to accept numeric values only in a textbox. – DTeuchert Apr 17 '19 at 20:00

2 Answers2

0

"^[0-9]+" . The ^ should be outside

Test12345
  • 1,625
  • 1
  • 12
  • 21
0

In the RegEx you need to define that between you start (^) and end ($) of the input, decimal characters only are valid, in this case the expression looks like

^\d+$

To check your specific requirements use Regex101 Online.

DTeuchert
  • 523
  • 8
  • 19