In this very case, using Regular Expression is a bad idea.
Do completely different thing. First of all, if you are using a text box, you can filter out input so the user could enter only numbers (and backspace, which is also considered a character, #8). This is easy to do with JavaScript, in your case. And when you need to use the value, perform validation. In a bit more general form, if will be something like this:
static bool Validate(string input, uint validMininum, uint validMaximum, out uint value) {
bool valid = uint.TryParse(input, out value);
if (valid)
valid = validMininum <= value && value <= validMaximum;
return valid;
}
I assumed
uint
, because you did not want to consider negative values, but you can use any other numeric type, for example,
ulong
.
—SA