CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Handling Validations Effectively With Validation Controls in ASP.Net Projects

By Satheesh babu
Posted On Dec 25,2008
Article Rating:
Be first to rate
this article.
No of Comments: 4
Category: ASP.Net
Print this article.

Handling Validations Effectively With Validation Controls in ASP.Net Projects

 

Validation is one of the indispensable parts of any asp.net projects we develop. To prevent any devastation or inconsistency due to user inputs we have to rely upon some sort of validations in asp.net application. Validations on user input can be done in 2 ways,

Ø       Client Side Validation

Ø       Server Side Validation

 

We cannot solely rely upon client side validation because there are chances that the javascript may be disabled on client machine. Also, there are some cases where we need to validate on users input based on some criteria or business rule that is calculated on server side due to the dependency on database etc.

It is always good to repeat some of the business critical validations on server side as a precaution. From 1.x days, asp.net is packed with a list of validator control that assists in easy validations in asp.net applications.These validation controls may or may not satisfy all our needs in the project. If you are an asp.net developer for quite sometime then you will have a basic idea on these validation controls. Not every time we can use these inbuilt validation controls as it is to serve our needs. This article will help us to use the asp.net validation control to the maximum extent to serve our project needs.

 

Validator control will fire on blur

By default, when we use asp.net validator control it will fire the validations on blur of the control. To provide a good user experience it is good if the validation controls are fired whenever submit button is clicked.

Read my previous article on Restrict Asp.Net Validator controls to Fire on Page Submit that speaks about restricting validator controls to fire only on submit. This approach will be good when the number of input controls are less.

 

Validating DropDownList with RequiredField Validator

We use RequiredField validator to validate an input control to be mandatory. When we use dropdownlist control it is not enough if we set the ControlToValidate property to the dropdown id. To make the RequiredField validator to work for DropDownList we need to set the InitialValue property to the default value of the DropDownList for doing mandatory check with RequiredField validator.

 

Refer the below code,

<asp:DropDownList ID="ddlRank" runat="server">

            <asp:ListItem>Select a Rank</asp:ListItem>

            <asp:ListItem>1</asp:ListItem>

            <asp:ListItem>2</asp:ListItem>

            <asp:ListItem>3</asp:ListItem>

        </asp:DropDownList>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1"

         InitialValue="Select a Rank" runat="server"

         ControlToValidate="ddlRank"

         ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

 

 

Validation Group in ASP.Net 2.0

The ASP.Net validation controls will fire for any postback caused by any of the controls on the page. To restrict this behavior for a control, say a Button, we need to set the CausesValidation property to false. Setting this property to false means we can’t use any validation control to validate the input for that button till ASP.Net 1.x. Also, ASP.Net 1.x lacks in providing a feature to make the validation controls applied to a group of controls. To answer this, Asp.net 2.0 introduced a new feature with validation control called Validation Group where we can validate group of controls separately.

Read my previous article about Validation Group in ASP.Net 2.0

 

Grouping All Validation Messages

Sometimes, we may get requirements to group all the validation messages at one place rather than on the side of the input controls. For example, the requirement may be placing all the validation messages on top or bottom of the pages. This can be achieved by the help of ValidationSummary control in asp.net.

Drag a ValidationSummary control to the place where we need to display the error messages and set all the Validator control’s Display property to none. This will make the validation messages to appear wherever we placed the ValidationSummary control.

 

Using Image as a Validation Message in Validation Control

We can also set some images as a validation messages when we use validator control. For example, we can show mandatory image as a validation message if a field is mandatory with RequiredField validator control.

 

Refer the below code that sets a image as validation message,

 

<asp:RequiredFieldValidator ID="RequiredFieldValidator1"

         InitialValue="Select a Rank" runat="server"

         ControlToValidate="ddlRank"

         ErrorMessage="<img src='error_arrow_bg.gif'>">

</asp:RequiredFieldValidator>

 

 




Conditional Validation with Validation Control

Sometimes, when we build an input processing forms we might come across situations where we need to validate some controls based on some conditions. For example, we can make a textbox to be required field only when a checkbox is checked or a dropdownlist value, etc. This is possible with CustomValidator control in asp.net.

 

Read my code snippet Conditionally validating a textbox when a checkbox is checked in ASP.Net that helps in implementing the conditional validation.

 

Using ServerSide Validation with Custom Validator

Like i said earlier, not all the validation can be done on client side. Validations like checking uploading file size when we use FileUpload control can be done on server side only or validations that depend on database can be done on server side only. Here comes the use of OnServerValidate event of CustomValidator which does a server side validation.

Read my code snippet on using server side validation with custom validator here,

Validate Image dimension when uploading in ASP.Net

File Size Validation in FileUpload control in ASP.Net

 

Re using Inbuilt Validation Control Scripts from JavaScript

Sometimes, we would like to reuse our validator control and call it from Javascript. i.e. explicitly calling a inbuilt validator control script. A practical example will be calling a validator that belongs to a different validation group from a button click that does not belong to that validation group.

Read my code snippet to call CompareValidator from javascript here.

 

Similarly, to call other validator controls refer the below table.

 

RequiredField Validator

RequiredFieldValidatorEvaluateIsValid(val)

 

Range Validator

RangeValidatorEvaluateIsValid(val)

 

RegularExpression Validator

RegularExpressionValidatorEvaluateIsValid(val)

 

Custom Validator

CustomValidatorEvaluateIsValid(val)

 

 

Where val is the id of the validator control.

 

Page.IsValid Property

Page.IsValid property will hold the input form validation status in asp.net. Checking this property on a server side event is a good practice because it prevents the users to submit a form that has failed validation but still posted to server because of script error or script execution disabled on the client browser.

Remember to call the Page.Validate(groupname) before checking Page.IsValid property when you check in a event that does not belongs to that validation group but still it requires to pass the validations on that group.

Conclusion

As validation is one of the important parts in our projects, Microsoft has made our life easy by giving some of the validation controls that fulfils our basic needs. Most of the time, we will need to do more than what these validator controls offers us and this article will help you in achieving some of those.

Thus, we have understood how we can use the existing validator control to the full extent so that our business requirement is fulfilled.

Happy Coding!!

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
About Article
Article is having good suggestions regarding validation
Thanks
got it working.....need to set the Value field as 'Select a Rank'
nipp
Good article.. but drop down concepts is not working
Thanks
Great Article,

Thanks.