HI WELCOME TO KANSIRIS

Asp.Net Validation Controls Tutorial

Leave a Comment

Validation Server Controls:

Validation Controls used for validating user input data, data format, datatype and range of value.

 Validation Types:

Validation is two types.
 1. Client Side
 2. Server Side.

Client Side Validation: 
 Performs validation without submitting the page to server(postback) with the help of scripting languages. Client Side validation is always preferable as it increases responsiveness of the webpage and users get instant feedback. But this is dependent on browser and Scripting language like javascript.
ServerSide Validation:
Developers prefer ServerSide validation as it is not dependent on browser and Scripting language.
You can use Asp.Net Server Controls which Provide both the Client Side and Server Side Mechanisms.

Asp.Net Validation Server Controls:

Asp.Net provides seperate controls for validating user input in a customized way. These Controls provide both server side and client side validations automatically. Validation Controls detects client browser for client-side validation.If it is enabled it adds javascript code for client-side validation if it is not then bypassed it to perform server-side validation. In the case of client-side validation when user clicks CausesValidation button the same error messages will display without submitting page to server.

Enable/Disabling Client-Side validation:
 we can enable/disable the client-side validation by setting enable/disable to EnableClientScript Property of the Validation Control. If client-side validation is set then page includes scripts to perform client-side validation.(WebUIValidation.js).

Disabling Client-Side validation in Asp.Net Page:

if you want to forcebly disable client-side validation on the page then set page attribute ClientTarget to DownLevel.

If you set ClientTarget attribute to UpLevel then forces controls to perform both validations.



Asp.Net Provides following controls to perform validation.



1. RequiredFieldValidator
2. CompareValidator
3. RangeValidator
4. ReqularExpressionValidator
5. CustomValidator
6.ValidationSummary


RequiredFieldValidator: 

This Control is simple validator control which checks whether data is entered in input control. Use RequiredFieldValidator to Make an input control as mandatory field.

RequiredFieldValidator Control in Asp.Net

This validation Control is used to make a field as mandatory in the form. Without filling the form user can't submit the form. Use the RequiredFieldValiator control to make an input control a mandatory field. The input control fails validation if the value it contains does not change from its initial value when validation is performed. This prevents the user from leaving the associated input control unchanged. By default Initial value is an empty string (""), which indicates that a value must be entered in the input control for it to pass validation.

Important Properties:

ControlToValidate: To which control this validation needs to apply.
InitialValue: Initial value of the control to which validation fails if you can't change this value.
ErrorMessage: Message to be displayed in validation summary control.
Text: Message to display in control. 
Display: When should be the error message displayed either static or dynamic.

 To use it:

1. Add two label, textbox and button control to form.
2. Now add requiredfieldvalidator control and set the controltovalidate to textbox,initialvalue,text and display properties.

Code looks like below
 

Button Click Event Code:

  
Form Work Like this:
RequiredFieldValidator Example


CompareValidator:

 Compares the value of one input control to another input control or a constant value.
CompareValidator Control is used to compare two values. The value to compare can be either a value of another control or a constant specified. There are predefined data types that can be compared like string, integer etc.

1).  Add a compareValidator control to the page and set the properties ControlToValidate, ErrorMessage,Text,Display.
2). Set the value to Compare by setting the properties like ValueToCompare, ControlToValidate, Type and Operator.

Age Validation using CompareValidator Control:

 

Comparing two passwords using CompareValidator Control:

 

Conclusion:

 

I hope you got an idea about how to use CompareValidator in Asp.Net Using Age and Phone Number validations.f

RangeValidator:

Checks the value of input control is in the specified region.
You can use the ASP.NET RangeValidator control to determince whether a user's entry falls within specific range of values - for example, between two numbers, between two dates or between alphabetic characters.
You set the upper and lower bounds of the range as properties of a RangeValidator Control. You must also specify the data type of the values that the control will validate.

To Use this:

1). Add a RangeValidator control to the page and set the properties ControlToValidate,ErrorMessage,Text and Display.
2). Set the low and high values of the range using MinimumValue and Maximumvalue properties.
3). Set the type property to specify the datatype of the range settings. use the ValidationDataType enumeration, which enables you to specify the following types
  • String
  • Integer
  • Double
  • Date
  • Currency

RangeValidator Example phone number and Date Validation:

 



RegularExpressionValidator:

Checks the value of the input control matches the specified pattern.

CustomValidator:

Allows you to write custom method to perform custom validation.

ValidationSummary:

Used for displaying summary of all errors on the webpage.

Conclusion:

I hope you got an idea about what are Asp.Net Validation Controls, Enable/Disable Client-Side Validation at Control level and page level. In my next article i will explain each Asp.Net Validation Server Control in detail.
         I would like to have feedback from my blog readers. Your valuable Suggestions,Questions,Comments and other requirements are always welcome.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.