I have a pattern and want to empty the textbox and show a message when the input does not match the pattern.
I have tried putting the erasing line in else
, and used a negation !
in front of the condition.
if (!Regex.IsMatch(MyTextBox.Text, ".*?[0-9].*?"))
{
MyTextBox.Text = "";
MessageBox.Show("Please input only numbers");
}
But both of these solution result in that if ANY symbol in the textbox matches the pattern gives an error when I want to float.parse
the string later on.
Examples:
- 1234 should and does work
- A Stops you as it should
- 1A Doesn't stop you, but should