Next


Validation Controls

 The validation controls are meant for performing client side validations.
 Def of Validation: The “Validation” is a process, that checks the user input values, whether those are correct or not. According to this, whenever the user clicks on “submit” button on the client system, the user-input values will be validated first. If all of those values are valid, then the page will be submitted to the web server. Otherwise, an error message will be displayed to the user and the page will not be submitted to the web server.
 Advantage of Validation: As an advantage of validation, invalid values containing pages would not be submitted to the web server. So that we can avoid the stress on web server.
 In older days, the client side validations are performed by using the code written in “java script” language. But in newer age of web development like ASP.NET, validation controls are introduced to replace the role of “java script validations”.
 Advantage of Validation Controls: Validation controls are very much easy to use. You don’t require to write even single line of code also to use them. So that we can avoid “java script” usage. In order to perform different kinds of validations, ASP.NET offers different types of validation controls.
 Functionality of Validation Controls: A validation control performs a particular type of validation on input / selection based controls like textbox, checkbox, checkboxlist, radiobuttonlist, listbox, dropdownlist etc. At run time, whenever the user clicks on “Submit” button it validates the particular control. If the given value is valid, it allows the page to be submitted. Otherwise, it stops the submition of the page and displays an error message on the page. By default, its forecolor is set to “red”.

Validation Controls in ASP.NET
A.	Client Side Validation Controls
    1)	RequiredFieldValidator
    2)	RangeValidator
    3)	RegularExpressionValidator
    4)	CompareValidator
    5)	ValidationSummary
B.	Server Side Validation Controls
    6)	CustomValidator

a) RequiredFieldValidator: Checks for the value entry in a particular control, and makes it as mandatory control. That means the user should enter the value in the specified field. Otherwise, it stops the submition of the page to the server. RequiredFieldValidator

b) RangeValidator: Checks the entered value whether it is in a given numerical range or not. If it is within the range, the value is valid. Otherwise, it is invalid. RangeValidator

c) RegularExpressionValidator: Checks the entered value whether it matches with a specified format or not. If it is matched with the specified format, the value is valid. Otherwise, it is invalid. Ex: Phone numbers, e-mail addresses, web site addresses, zip codes, postal codes etc. RegularExpressionValidator

d) CompareValidator This can be used in 3 models.
1. Comparison with a fixed value.
The control’s value will be compared with a fixed value. Comparison with a fixed value

2. Comparison with another control’s value.
The control’s value will be compared with a another control’s value. Comparison with another control

3. Data Type Comparison. Checks the value for a specified data type. It doesn’t compares with any other value. Data Type Comparison

e) ValidationSummary: This doesn’t performs any individual validation. It is designed for the pages, having more no. of validation controls within the same page. At the time of submition, this controls takes the error messages from other validation controls and re-displays them in a bulleted format. You don’t require to set any properties for this control.
f) CustomValidator: Used to perform some other external type of validations, which includes with some code to be executed. For example, in a form, there is a textbox, to enter a filename. The validation is to not to enter non-existing file named. So that, there is a necessity of validating the file name, whether it actually exist on the disk or not. At that time, you have to use “CustomValidator”. But unlike other validation controls, this requires to write some code for validation. The validation code can be implemented in “ServerValidate” event. CustomValidator

Demo on Validation Controls

Validation Controls


Default.aspx.cs
using System.IO;
    protected void custValidatoryFilePath_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string filename = args.Value;
        FileInfo fobj = new FileInfo(filename);
        if (fobj.Exists)
            args.IsValid = true;
        else
            args.IsValid = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid) //this condition is required only when, you are using "CustomValidator" in the page.
            Response.Redirect("success.aspx");
    }
submitonserver