Validation controls in ASP.NET

Data validation is very important while accepting input from user. There may be different business logic for validating data. Some basic validation would be to validate a field to accept only numbers or a valid email address or phone number of a country in a specific pattern etc. ASP.NET provides some built-in controls for this purpose called validation controls. There six validation controls in ASP.NET

ASP NET Validation Controls

  • RequiredFieldValidator: RequiredFieldValidator ensures that a field is not blank. It can be applied to a field that is required to be filled.
  • CompareValidator: CompareValidator is used to compare values of two fields. For example, it can be used to validate password and confirm password fields in a registration form.
  • RangeValidator: RangeValidator is used to check whether the value of a field is in a specific range. For example, it can be used to validate that value in age field should be between 18 and 25.
  • RegularExpressionValidator: RegularExpressionValidator is used to validate value of the field against a regular expression.
  • CustomValidator: If your validation logic does not matches any of the above validation controls, you can write a custom validation logic using CustomValidtor control.
  • ValidationSummary: ValidationSummary is used to display the summary of all the validation errors on the page.

Every validation control except ValidationSummary has a ControlToValidate property. To bind a validation control to a field we need to set ID of the control to the ControlToValidate property. In case of validation fails we need to display error message to the user. ErrorMessage property of the validation controls is set to the error message that we want to display when a field is not validated. When validation of a field is failed, Page.IsValid property is set to false. Therefore we should always check Page.IsValid property before submitting the form.

RequiredFieldValidator Example

Let us take an example of RequiredFieldValidator control. We will validate a TextBox control against a RequiredVieldValidator which will ensure that the TextBox is not left blank

<div style="width:400px">

    <br />
        onclick="btnSubmit_Click" />

Here, we set ID of the TextBox control to the ControlToValidate property of RequiredFieldValidator control. We also set ErrorMessage property of the RequiredFieldValidator to the error message to be shown to the user on error. By default, color of the error message is gray in ASP.NET 4.0. So, we set ForeColor property of the validator to display error message in red.

protected void btnSubmit_Click(object sender, EventArgs e)
    if (Page.IsValid)
        // Submit the form
        Response.Write("Form submitted");

In case of a failed validation, Page.IsValid is set to false by default. So, before submitting the form we check if Page.IsValid property is true in the Button’s Click event. A failed validation will look something like following

Home Page - Mozilla Firefox_2013-08-02_00-02-23

Leave a Reply

Your email address will not be published. Required fields are marked *