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.
-
You dont want to use range validator??AliRıza Adıyahşi– AliRıza Adıyahşi2012-08-01 07:31:32 +00:00Commented Aug 1, 2012 at 7:31
-
want to do by regular expression validator onlyShri– Shri2012-08-01 08:04:39 +00:00Commented Aug 1, 2012 at 8:04
Add a comment
|
3 Answers
"^[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
}
6 Comments
Grhm
I think less than 1000 means 3 digits max, therefore {1,3} should be {1,2} as you've alreay got the first digit from the [1-9].
Mazdak Shojaie
This code return ArgumentException was unhandled:parsing "^[1-9][0-9]*{1,2}$" - Nested quantifier {.
Mazdak Shojaie
a ) is needed at the end of first line
Shri
but i want validate expression
Shri
same here return argument exception
|
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
1 Comment
Mazdak Shojaie
The
((?<=\d)0|[1-9]){1,3} return true for value greater than 1000