-1

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!

3 Answers3

1

By no means a RegEx expert, but maybe:

^(\d|\d+[.,]?\d+)$

This would test for either:

  • \d - A single digit
  • \d+ - One or more digits with [.,]? zero or one dot or comma followed by \d+ at least one more digit.

Online Test

JvdV
  • 70,606
  • 8
  • 39
  • 70
0

I think your problem might be that regex.IsMatch(Text) returns true if there is a match anywhere in the string. See this post for an explanation how to match the whole string.

backspace
  • 124
  • 1
  • 9
0
    private static bool validateFloatNumber(string Text, TextBox Box)
    {
        Regex regex = new Regex("^[0-9]{1,9}([,.][0-9]{1,4})?$");
        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;
    }

^ Beginning Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled

[ Character set. Match any character in the set.

0-9 Range. Matches a character in the range "0" to "9" (char code 48 to 57). Case sensitive.

]

{1,9} Quantifier. Match between 1 and 9 of the preceding token.

(

Capturing group #1. Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.

[ Character set. Match any character in the set.

, Character. Matches a "," character (char code 44).

. Character. Matches a "." character (char code 46).

]

[ Character set. Match any character in the set.

0-9 Range. Matches a character in the range. "0" to "9" (char code 48 to 57). Case sensitive.

]

{1-4} Quantifier. Match between 1 and 4 of the preceding token.

)

? Quantifier. Match between 0 and 1 of the preceding token.

$ End. Matches the end of the string, or the end of a line if the multiline flag (m) is enabled.

Vignesh Nethaji
  • 374
  • 3
  • 15