How and when to use RangeValidator is a tricky question, can we use RangeValidator to validate a string or date, most of us say we can not use RangeValidator for string but we can, we will see How to use RangeValidator for string?, How to use MinimumValue and MaximumValue for type String? ASP.net provides easy to use control to validate a control value between the range. It can be used to validate values of data type Currency, Date, Double, Integer and String, we will see this in detail in this article.
<asp:RangeValidator ID="RangeValidatorID" ControlToValidate="Control ID to which you want to validate" MinimumValue="value" MaximumValue="value" Type="DataType" ErrorMessage="Error Message, if invalid" Text="Message to display in control" ForeColor="Red" BackColor="White" runat="server" > </asp:RangeValidator>
Range can be defined by MinValue and MaximumValue.
Currency: A decimal data type that can contain currency symbols.
Date: A date data type.
Double: A double-precision floating point number data type.
Integer: A 32-bit signed integer data type.
String: A string data type.
Let's say you want to validate a text box to accept number between 1 to 10
Enter number (between 1 - 100) : <asp:TextBox ID="txtNumber" runat="server" /> <asp:RangeValidator ID="rvNumber" runat="server" ControlToValidate="txtNumber" MinimumValue="1" MaximumValue="100" Type="Integer" ForeColor="Red" ErrorMessage="Invalid Quantity" />
Now txtNumber text box will only accept number between 1 to 100, if you will try to enter any number greater than 100 it will show you the error, or less than 1 even then it will show error.
It can not accept any string or anything else except number between 1 to 100 (inclusive)
I try to understand how it can be used for string but could not get anything which can validate for range even I googled to get How to use RangeValidator for string?, How to use MinimumValue and MaximumValue for type String? but could not get any satisfactory answer so tried my own and found we can use it in some scenario say you want to accept only lower case or only upper case characters for a particular text box then we can use as bollows (for upper case)
<asp:TextBox ID="txt1" runat="server" /> <asp:RangeValidator ID="rvNumber" runat="server" ControlToValidate="txt1" MinimumValue="A" MaximumValue="Z" Type="String" ForeColor="Red" ErrorMessage="Must be in capital" />
In this case our txt1 TextBox can accept anything but only in upper case if you want to accept only in lower case then use a and z for MinimumValue and MaximumValue.
If we will use MinimumValue="A" MaximumValue="A" then text box will accept only A
If we will use MinimumValue="A" MaximumValue="B" then text box will accept any number of character but only in upper case.
If anyone have better idea please suggest us.
|By Tigers On 29 Dec, 12 Viewed: 2,137|
In many cases we need to use read only text box so user cannot edit the value but readonly textbox will lost its value after postback. Let’s say you have a read only text box to set date by using ajax calendar so after post back date will be lost, other case might be you are setting some value in... By Ali Adravi On 24 Apr 2013 Viewed: 4,122
Most of the times we need to use GridView control to show tabular data in our asp.net application. We simply write procedure to search the records and bind them with GridView and show 10 to 20 records per page. Have you ever thought that why you are fetching all the records and show only 10 to 20... By Ali Adravi On 16 Feb 2013 Viewed: 8,194
A nice feature with ASP.Net is the model binding, it reduced our code and effort to bind our well know controls like GridView, DataList, Repeater etc. So let’s see how we can bind our old controls in new and easy way. Let’s see how we bind the our grid before 4.5 1. We write the code to get... By Mike .Net On 17 Jan 2013 Viewed: 3,150
In asp.net there is not control to select multiple files and upload them once with progress bar, so we will use a small third party DLL to achieve this functionality. We will use Flajaxian FileUploader, you can download it from [http://www.flajaxian.com] We will create three different images... By Hamden On 12 Jul 2012 Viewed: 6,503