0

I have a field on my c# mvc model to hold percentage - I want to use RegEx data annotation to validate that the field only contains values of 0-100

I seen a similar Regex here - ^([1-9][0-9]{0,2}|1000)$ - that matches values 1-1000 - I am not sure of the correct ammendment to make it fit for my scenario?

I tried something like below ^([0-9][0-9]{0,2}|100)$ but not working as expected?

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
Ctrl_Alt_Defeat
  • 3,933
  • 12
  • 66
  • 116

6 Answers6

5

Why do you want to validate it using a regex? I think it would be easier to just use .TryParse and a range check:

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

Also more readable if you ask me …

knittl
  • 246,190
  • 53
  • 318
  • 364
  • 1
    @AvinashRaj I mean they constitute an inappropriate, albeit technically correct, solution to the problem. You can describe a correct technique for using a chainsaw to open a tin of soup, but it remains nevertheless an unsuitable tool (and approach) for the job. – J... Aug 19 '14 at 09:35
4

You could use the below pattern to match the digits from 0 to 100,

^(?:100|[1-9]?[0-9])$

DEMO

Explanation:

  • ^ Asserts that we are at the start.
  • (?:) Non-capturing group which only does a matching operation.
  • 100 Matches the string 100.
  • | Logical OR operator used to OR two regexes.
  • [1-9]? Matches one of any digit from the range 1 to 9. ? after the character class makes it optional.
  • [0-9] Matches one of any digit from the range 0 to 9.
  • $ Asserts that we are at the end of the line.
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • +1, Though the shortest approach would be `^(?!0)\d{3}$`. – Unihedron Aug 19 '14 at 09:59
  • 2
    This solution is in excess of 500% more computationally intensive than a simple `TryParse` and range check. Don't write validation code like this. It's ugly, nonintuitive, hard to read, and incredibly inefficient. There is no sensible reason to choose regex to solve this problem. – J... Aug 19 '14 at 10:22
  • 2
    @J... [There is.](https://www.thc.org/root/phun/unmaintain.html) – Unihedron Aug 19 '14 at 10:40
  • @J... - I am wanting to make use of Data Annotations above the property in Question - hence using RegEx – Ctrl_Alt_Defeat Aug 19 '14 at 12:38
  • @Ctrl_Alt_Defeat There is a range attribute you can use that is rather more appropriate - again, why regex? – J... Aug 19 '14 at 12:49
  • @J... I think originally I had used Range but my requirement for percentage field is whole numbers onlly - I belive Range allowed 2.5 for example which I did not want – Ctrl_Alt_Defeat Aug 19 '14 at 21:22
  • @Ctrl_Alt_Defeat if it is an integer property then it cannot have a non-integer component. – J... Aug 19 '14 at 22:01
2

If the percentage field on your model is of type int, you could more easily use the Range annotation:

[Range(0, 100)]
public int Percentage { get; set; }
WeSt
  • 2,628
  • 5
  • 22
  • 37
0

Something like so should work: ^([0-9]{1,2}|100)$. That being said, please refrain from using regular expressions for number evaluation. You should use numeric operators for this purpose.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

You can do it like this:

^(([1-9]\d?)|100)$ This would ensure that

There is at least one digit

That multi-digit numbers start in a digit other than zero

That the only allowed three-digit number is 100.

Jay Nirgudkar
  • 426
  • 4
  • 18
0

This will work:

^([1-9]?[0-9]|100)$
arco444
  • 22,002
  • 12
  • 63
  • 67