I have a required validation setup on a textbox, but I also have to make sure it is an integer.
How can I do this?
RegularExpressionValidator. The RegularExpressionValidator allows validating the input text by matching against a pattern of a regular expression.
If all that you are concerned about is that the field contains an integer (i.e., not concerned with a range), then add a CompareValidator
with it's Operator
property set to DataTypeCheck
:
<asp:CompareValidator runat="server" Operator="DataTypeCheck" Type="Integer" ControlToValidate="ValueTextBox" ErrorMessage="Value must be a whole number" />
If there is a specific range of values that are valid (there probably are), then you can use a RangeValidator
, like so:
<asp:RangeValidator runat="server" Type="Integer" MinimumValue="0" MaximumValue="400" ControlToValidate="ValueTextBox" ErrorMessage="Value must be a whole number between 0 and 400" />
These will only validate if there is text in the TextBox, so you will need to keep the RequiredFieldValidator
there, too.
As @Mahin said, make sure you check the Page.IsValid
property on the server side, otherwise the validator only works for users with JavaScript enabled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With