2

I've tried to use the following RegEx annotation, but it didn't work:

[RegularExpression(@"([0-9]|[0-9]\d|100)$"]

(this answer suggest that it's a bad approach)

I also tried with maskedInput 1.2.2 (jQuery), but it won't accept any single digit value

jQuery(function ($) {
    $("#MyControl").mask("99"); //also tried "9?9"
});

Is it ever possible to have a mask accepting value from 1 to 100 only?

Community
  • 1
  • 1
Mathieu
  • 4,449
  • 7
  • 41
  • 60
  • Are you talking about data validation or are you talking about input typing restriction to not allow people to type in i.e. "abc" in an input field? Because these two are different and none of the server-side data annotations will help you with the latter. – Robert Koritnik Oct 23 '12 at 19:09
  • @RobertKoritnik The sooner the validation, the better for me. So if it's possible to prevent the user from entering anything other then a number from 1 to 100, that'd be nice! – Mathieu Oct 23 '12 at 19:15

2 Answers2

8

Have a look at the RangeAttribute.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • RangeAttribute is not generating validation as far as I'm concerned – Andre Pena Oct 23 '12 at 19:05
  • 3
    @AndréPena: You're wrongly concerned. `RangeAttribute` does set entity property constraints that are being validated. Could be that you've used it on a wrong data type... Similar to what Mathieu likely did. He likely put regular expression on an `int` property which of course didn't do the trick. – Robert Koritnik Oct 23 '12 at 19:06
1

The short answer is "no", since there is an infinite representation of the integers 1-100 (1, 01 , 001, 0001, etc., are all valid representations of the number 1.

You could restrict data entry to the canonical representation (1-9, 11-99, 100) like this:

(100|([1-9][0-9])|[1-9]

That should do the trick.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
  • Amongst the answers, this will validate first. The form doesn't need to be sumbitted for the user to see that his input is wrong – Mathieu Oct 23 '12 at 20:15