I have successfully filtered out anything but digits with the following code. I have utilized the PreviewTextInput
event to capture the current input:
private void NumericValidation(object sender, TextCompositionEventArgs e)
{
e.Handled = !_numericValidator.IsMatch(e.Text);
}
_numericValidator
is defined as _numericValidator = new Regex("^[0-9]+$");
This code works perfectly fine to only allow 0-9 to be entered into the textbox.
However, I am looking to allow a single decimal so input can be more precise. I have tested the following regex in RegexBuddy and it works as I expect it should but I can input invalid numbers such as 1..1
and 4.3.5
into the text box.
^(?!\.{2,})(?:\d+)?(?:\.{0,1})?(?:\d+)?$
Any suggestions as to what I should try next?