tags:

views:

198

answers:

3

How do I restrict single zero in a numeric textbox? Textbox can accept any number but it should not accept only zero as value.

Example: "1984500" is valid but "0" is invalid.

Edited:

Is it possible to give maximumvalue as infinity?

+2  A: 

Using a RegularExpressionValidator with the following regex: \d*[1-9]\d*

Yuriy Faktorovich
+4  A: 

I'd use a RangeValidator and set the minimum value to 1.

<asp:RangeValidator ID="range1" RunAt="server"
                    ControlToValidate="Textbox1"
                    MinimumValue="1"
                    MaximumValue="2147483647"
                    Type="Integer"
                    EnableClientScript="false"
                    Text="The value must be greater than 0" />
tvanfosson
Agreed; this is a better way than the RegEx because the RegEx will still allow an invalid number to be entered.
Noon Silk
What bad number could be entered by that RegEx? Although personally I'd go with the range too.
Yuriy Faktorovich
Is it possible to give maximumvalue as infinity
Geetha
@Yuriy: You can enter a number that is larger then the allowed value for an int (999999999999999999999999, say). Geetha: No.
Noon Silk
@silky: int isn't in the question.
Yuriy Faktorovich
@Geetha -- if you don't specify a maximum value, then it simply doesn't do the maximum check. Any number specified would still need to convert to whatever numeric data type you are using, however, so technically you can't specify infinity (I think this is true even if you use double as the type as there is no representation of infinity that will convert).
tvanfosson
I have tried without using maximum value but it showing error.Maximum value canot be converted to the type integer
Geetha
Interesting. I could have sworn it wasn't required. You could always set it to the maximum integer value: 2,147,483,647.
tvanfosson
ok. Thank You for your response.
Geetha
A: 

This should take care of it. Allows a single digit 1-9 or a collection of digits 2 or more.

[1-9]|\d{2,} 
Joel Etherton
I'm not sure 00 should be allowed.
Yuriy Faktorovich