0

How do I restrict special characters and character in a Textbox? I'm using this code but I am not restricting special characters and character

code :-

if (!Regex.IsMatch(((Windows.UI.Xaml.Controls.TextBox)sender).Text, @"^\\d*\\.?\\d*$"))
{
    // Write Code 

}
Bugs
  • 4,491
  • 9
  • 32
  • 41
Awaneesh
  • 11
  • 4
  • You may want to check this [answer](http://stackoverflow.com/a/19524692/4466640). However this does probably the other of what you need. Make the `TextBox` accept letters and numbers. Just to give some idea. – Hexxed Mar 01 '17 at 08:20

1 Answers1

5

if you're using @ in front of string, you don't have to escape characters. So, remove all those extra backslash chars and your regex should work. Like this:

if (!Regex.IsMatch(((Windows.UI.Xaml.Controls.TextBox)sender).Text, @"^\d*\.?\d*$"))

EDIT: use sites like this to test your regex.

Nino
  • 6,931
  • 2
  • 27
  • 42