Wednesday, September 3, 2008

Asp .Net Validation Controls

Client Side validation is an important task in Web Application development. All server side technologies except Asp .Net expects the programmer to be conversant with JavaScript, the default client side scripting language. Asp .Net comes packed with a number of validation control which emits javascript thus making the life of programmer easier.

RequiredFieldValidator can be used to check whether a value is entered into a control.
Place a RequiredFieldValidator next to the control you want to check for the extistence of value.
Set the ControlToValidate Property to the Control you want to validate. Set an appropriate errror message using the ErrorMessage property. If you submit the form without entering the a value to the TextBox the use will receive an error message.


RequiredFieldValidator InitialValue

Suppose you want to display an additional message inside the textbox and that text shouldn't be treated as an input text. Then you can set the InitialValue of RequiredFieldValidator to the text that is displayed in the textbox.

CompareValidator can used for :
i. checking the type of contents in a control, for example whether the value entered is numeric.
ii. Check for equality of the contents in two controls, for example checking the text of Password and Reenter Password textboxes.
iii. Checking whether the values of two controls are greater than, lesss than, etc. for example wheter the value entered in Bid textbox is greater than the value in Price textbox.

OperatorCheck Property

i. Lets take up the first scenario. We want to check whether the value entered into a control is numeric.

As usual add a CompareValidator next to the control you want to validate. Set the ControlToValidate and ErrorMessage as in the case with RequiredFieldValidator. Set the Operator property to DataTypeCheck and Type to integer.

Now if the end user happens to enter a non numeric value he will receive an error message specified in the ErrorMessage property.

ControlToCompare Property

ii. Let's take up the scenario of checking the equality of values entered in two controls as in the case with Password and Reenter password textboxes.

Here set the ControlToValidate property to Re enter password textbox's id and ControlToCompare to the password textbox's id.

iii. In the case of third scenario where the Bid Amount should be greater than Price amount, set the ControlToValidate to Bid textbox's id and ControlToCompare to Price textbox's id. Next set the Operator to GreaterThan and Type to integer.

can be used to check wheter the value entered into a control falls under a specific range. For example we can check wheter the age is between 35 and 60. In this case apart form ControlToValidate and ErrorMessge set the MinimumValue and MaximumValue property to 35 and 60 respectively.


Instead of displaying error message side by side the textbox, it can be displayed as summary. For this you can use ValidatiorSummary control. Place a ValidationSummar Control on the form having validation controls. Set the Text Property of Validation Controls to say *. All the controls that fail the validation text will display * as shown in the figure.


  1. Hi

    i have 10-15 textboxes to enter numeric values. How will i check whether the data entered in the textbox is numeric value or not. i tried the reange validator.but the problem is that even after clicking the save button it shows the error msg.can u pls help me out..

  2. Hi Shalvin,

    ur note on validation controls was very useful for me...

    thanx a lot

  3. Inorder to check whether numeric value is entered into a control
    Use CompareValidator Set the Operator property to DataTypeCheck and Type to integer.

  4. hi,
    thanx for ur reply on validation controls....i'm using sql server 2000 db. i want to display the id and serial number in textbox.ID should be primary key and auto increment also serial number should be auto increment but should start with 1001. how can i do it in (c#).i want the incremented ID and Serial number to be displayed in the text box wen the user tries to make a new entry.