locked
Regex RRS feed

  • Question

  • User-1499457942 posted

    Hi

      In below Regex i want that range should be [1 to 100]

    public static bool IsDecimal(string input)
    {
    return Regex.IsMatch(input, @"[0-9]+(\.[0-9]+)?");
    }

    Thanks

    Monday, September 10, 2018 5:43 AM

All replies

  • User1724605321 posted

    Hi JagjitSingh,

    You can try :

            public static bool IsDecimal(string input)
            {
                return Regex.IsMatch(input, @"^[1-9][0-9]?$|^100$");
            }

    Best Regards,

    Nan Yu

    Monday, September 10, 2018 6:06 AM
  • User-1499457942 posted

    Hi Nan Yu

      I have added 80.00 , even then it is saying Invalid value

    Thanks

    Monday, September 10, 2018 8:50 AM
  • User1724605321 posted

    Hi JagjitSingh ,

    If the string is not fixed , it will be 80 or 80.00 or any other input , i suggest you could try solution without regex, for example :

    bool Validate(string input) {
      int x;
      return int.TryParse(input, out x) && x > 0 && x <= 100;
    }

    Best Regards,

    Nan Yu

    Tuesday, September 11, 2018 3:24 AM