I am currently testing input validation for a project, and i am using regex.
I am trying to see if the content of a TextBox is a decimal number before i am trying to parse it.
For this I am using the expression \d+([\.\,]{1}\d+)?
I expect it to check if there are one or more digits, then zero or one of the combination of either a point or a comma and one or more digits.
Valid inputs would be:
123
12.3
1,23
Invalid inputs would be:
12.
.123
1...2
1.2.3.3
After testing it with different, known wrong inputs, it seems like the expression does not work as i would expect it to. Am I missing something?
The code i am using is:
private bool validateFloatNumber(string Text, TextBox Box)
{
Regex regex = new Regex("\\d+([\\.\\,]{1}\\d+)?");
bool result = regex.IsMatch(Text);
if (!result)
{
if (Text.Length == 0)
{
Box.Background = Brushes.Gray;
}
else
{
Box.Background = Brushes.Red;
}
}
else
{
Box.Background = Brushes.Transparent;
}
return result;
}
I tested it with the input 1...1
and the value written in results
were true
Here is an image of the debug and the wrong value given in result
Thanks in advance!