I would like to know how I would be able to allow only numbers and a "-" minus sign in a textbox?
Here is coding that I can already allow only numbers:
private void txtDicountSettlement_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
Just add the -
to your regex character group, in a position that's not making a range of characters:
private void txtDicountSettlement_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9-]+");
e.Handled = regex.IsMatch(e.Text);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With