Friday, June 5, 2009 Range Validator Control

In this post I will try to explain you how to use the 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.
Data Type
StringA string data type
IntegerA 32-bit signed integer data type.
DoubleA double-precision floating point number data type
DateA date data type. Only numeric dates are allowed. The time portion cannot be specified..
CurrencyA 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.

Double: The Double validator checks if the given input is double or not. If the input is not double, it generates the error message. Double validator can also be used to check the input range. RangeValidator does not use a regularexpression to validate a double. It uses a regular expression to extract the digits and decimal character out of the string. It then converts the result into an actual number using the javascript parseFloat() function. If that function cannot convert, the RangeValidator knows the value was not acceptable to javascript.(Source)

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!


Anonymous said...

Unfortunately, the RangeValidator has a bug and won't accept currency symbols like the $. This makes it pretty much useless, IMO. Microsoft acknowledges the bug:

David Salahi

Asim said...

Anonymous: Thanks for your informations.