1

I have a textbox that a user enters a number into. I need to ensure that the number is at most 5 numbers before the decimal place and mandatory 2 digits after. The number must always have 2 digits after the decimal point. What Regex could I use to check this? (The solution is in C#)

Madeleine
  • 2,152
  • 2
  • 24
  • 35

2 Answers2

7

Something like this:

  String source = ...;

  if (Regex.IsMatch(source, @"^[0-9]{,5}\.[0-9]{2}$")) {
    //TODO: put relevant code here
  }                 

If you want at least one digit before decimal point, the pattern will be

  @"^[0-9]{1,5}\.[0-9]{2}$"
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

Just Try this code

        string Value= "12345.63";
        if (Regex.IsMatch(Value, @"^[0-9]{5}\.[0-9]{2}$"))
        {
            Console.WriteLine(Value);
        }
        else
        { 
            Console.WriteLine("Not Match");
        }
        Console.ReadKey();
Badruzzaman
  • 41
  • 1
  • 7