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.

Asp .Net Ajax VII : AlwaysVisibleControlExtender

With Ajax .Net we can easily make a part of the page always always visible. An example of Always visible content is the gmail's Sending message as you sent a message. Even if you scroll down the page the Sending message will be visible.

Lets see how to implement it in Asp .Net.

Open an Ajax Web Page.

Add a Panel set a BackColor and a Text.

Add an AlwaysVisibleControl Extender. Set the TargetControlId to the Panel control and HorizontalSide to Right.