Search code examples
c#asp.netregexvalidation

One to Infinity regular expression


I am trying to validate my text box control, which should hold a number between one and infinity ( it cannot be 0 or a negative number).

I would like to use a regularexpressionvalidator in c#, as a rangevalidator asks for a maximum number when I don't want there to be one.

I was hoping if anyone could assist me to create a regular expression that checks for a number between one and infinity.

I have searched online and the closed thing I have found is {1,}, but this causes a parsing "{1,}" - Quantifier {x,y} following nothing error. Not too clued up on regular expressions but was hoping someone could help me find an answer.


Solution

  • If you just need a whole number,

    [1-9][0-9]*
    

    should have you covered. That's "a non-zero digit, followed by any number of digits".

    If you don't want to reject e.g. 04546, something like

    0*[1-9][0-9]*
    

    should do it. (Same as above, preceded by zero or more zeroes.)