Sunday, December 23, 2012

ASP.NET Validation Controls: Range Validator


Range validator control in ASP.NET makes it possible to validate a specified range.

This range could be

  • String
  • Integer
  • Double
  • Date
  • Currency
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" MaximumValue="d" MinimumValue="a" Text="*" Type="String"></asp:RangeValidator>
<asp:Button ID="Button2" runat="server" Text="Submit" />

In the above example the text starting from a to d will only be allowed. You can change the below properties.

  • ControlToValidate
  • MaximumValue
  • MinimumValue
  • Type
Below example will allow user to enter values only between 0 and 100.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" MaximumValue="100" MinimumValue="0" Text="*" Type="Integer"></asp:RangeValidator>
<asp:Button ID="Button2" runat="server" Text="Submit" />

Note: Empty text will not be validated by the control. You'll need to add a Required Field validator to validate the empty text.

No comments:
Write comments
Recommended Posts × +