In this post I will try to explain you how to use the asp.net range validator control.The RangeValidator control is used to check that the user enters an input value that falls between two values. It is possible to check ranges within Character, Integer, Double , Dates, and Currency. For this example I have used all the range check options.
The RangeValidator control uses four key properties to perform its validation. The ControlToValidate property contains the input control to validate. The MinimumValue and MaximumValue properties specify the minimum and maximum values of the valid range.The BaseCompareValidator.Type property is used to specify the data type of the values to compare. The values to compare are converted to this data type before the validation operation is performed. The following table lists the different data types that can be compared. Here is the list of type.
|String||A string data type|
|Integer||A 32-bit signed integer data type.|
|Double||A double-precision floating point number data type|
|Date||A date data type. Only numeric dates are allowed. The time portion cannot be specified..|
|Currency||A decimal data type that can contain currency symbols.|
Note Validation succeeds if the input control is empty. Use a RequiredFieldValidator control to make the input control a mandatory field.
Note The RangeValidator control throws an exception if the value specified by the MaximumValue or MinimumValue property cannot be converted to the specified BaseCompareValidator.Type.
After some information about what the range validator do and what are the most important properties of the range validator control. Now let us start with our example which has the following screen. You can see in the screen that I have use each of the type used by range validator to validate the type, string , Integer, Double, Date and Currency. Let us start with each of the type.
String: When you have text box which is used to take input of type string and you want to validate the input string enter by the user then the type of range validator will be string.The RangeValidator only checks to see if a value is within a given range.In case of type string for the range validator it will only check the character by character, not the length of the string. For example in my case I have set the range for the range validator for the type string is Minimum Value To 'A' And maximum value to 'z' (small z) then it will check whether the input character is within the range of minimum and maximum value.
Integer: Next type which is place in the screen is the integer data type.To validate text box for the integer type what you have to do is to attach the range validator and set the minimum and the maximum value for the range validator and also set the the type to the integer type. In the example above I have set the minimum value to the 2 and maximum value to 10 so I am restricting the user to enter value between this range where user can enter minimum value to 2 and the maximum value of 10.
Date: In order to validate the date for input text box, you can set the type of the type to validate for the range validator to date. And also set the minimum and the maximum value of the date range. The date will be validate if the user enter the date which will be in the range of the minimum and maximum value of the range validator control.
Currency: if you want to validate the currency for a text box then you have to set the type to the currency in the range and set the minimum and the maximum value of the range validator control.It will validate the input value to the 2 decimal places.
You can download the source code from here.
All and any comments / bugs / suggestions are welcomed!