Reputation: 29
I want to validate the text within a textbox using a regular expression.
The text should be a number greater than 0 and less than and equal to 1000.
Upvotes: 2
Views: 3056
Reputation: 1698
I found it:
^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$|^(1000)
I test it in range 0~1000
Upvotes: 0
Reputation: 10367
Try this regex:
//for 0 < x < 1000
^((?<=[1-9])0|[1-9]){1,3}$
explain:
(?<=[1-9])0 //look behind to see if there is digits (1-9)
test:
0 -> invalid 000 -> invalid 45 -> valid 5 -> valid 'Ashwin Singh' solution can not capture this 101 -> valid 999 -> valid 1000 -> invalid 12345 -> invalid 10000 -> invalid 2558 -> invalid 205 -> valid 1001 -> invalid 2000 -> invalid
And better way convert to Decimal
(if you dont use regular expression validator):
Decimal dc = Decimal.TryParse(textBox.Text);
if( dc > 0 && dc < 1000)
// do some thing
Upvotes: 0
Reputation: 7345
"^[1-9][0-9]*{1,2}$"
is the regex you are looking for.
if(Regex.IsMatch(YourTextBox.Text,"^[1-9][0-9]*{1,2}$"))
{
//Write your logic here
}
Upvotes: 3