Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net validation to make sure textbox has integer values

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?

like image 925
mrblah Avatar asked Sep 15 '09 14:09

mrblah


People also ask

Which Validator is used for value requirement in TextBox?

RegularExpressionValidator. The RegularExpressionValidator allows validating the input text by matching against a pattern of a regular expression.


1 Answers

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.

like image 66
bdukes Avatar answered Sep 24 '22 00:09

bdukes