views:

365

answers:

1

If I want to validate that a text box contains an integer greater than or equal to zero. Do I need to use TWO asp:CompareValidator controls: one with a DataTypeCheck operator and one with a GreaterThanEqual operator?

Or is the datatype operator redundant? Can I just use a single validator with the GreaterThanEqual operator (and the type set to Integer)?

+3  A: 

This should be enough

<asp:RangeValidator id="Range1"
           ControlToValidate="TextBox1"
           MinimumValue="0"
           MaximumValue="2147483647"
           Type="Integer"
           Text="The value must be integer and greater or equal than 0"
           runat="server"/>
Claudio Redi
Actually I think I like the idea of using a rangevalidator better because you set an upper bound, but any particular reason you don't use a compare validator?
User
@User: I don't think that compare validator is appropriated for your scenario. From MSDN -> "CompareValidator Class: Compares the value entered by the user in an input control with the value entered in another input control, or with a constant value."
Claudio Redi
Well you can set the constant value to 0 and use GreaterThanEqual which I think is pretty normal usage.
User
Ok, but you have to be sure that you disallow inputs bigger than the biggest INT. That's all the difference. If you handle that situation properly then with only one CompareValidator should be enough
Claudio Redi