Validation Control in ASP.NET

If you have went through my previous article on Validation control then kindly go through the following link

http://www.c-sharpcorner.com/UploadFile/17e8f6/requiredfieldvalidator-control-in-Asp-Net/

Validation controls, if used properly, can be very useful for reducing large, complex, time-consuming JavaScript code. There are various validation controls provided by ASP.Net. We will have a look at them one by one.

For demonstrating the validation control I'm going to use a registration form where the user is requested to fill in the following fields: Name, Address, Country, Email, word, confirm word and Age. If the user fulfills all these details successfully then there will be no validation error, otherwise the user will be presented with the validation errors for the page. Kindly have a look at the following page.

  1. RequiredFieldValidator

    As the name suggests, it should be used on those fields that are mandatory. There are various properties of this field that we need to set; they are:

    • ControlToValidate: the name of the target control whose value needs to be checked whether it has a value or not.
    • ErrorMessage: Error Message to be displayed if validation fails.
    • Text: Text to be displayed if the target control does not hold any value.
    • InitialValue: This is basically is for controls such as DropDownList whose value needs to be checked to determine whether the value is greater than the initial value of the required field validator.

    For instance in our example our dropdowlist has the 4 values Select, India, Australia and China. We want a validation where a check is to be done to determine whether the selected index of the drop down is >0. In such cases we have to specify the initialvalue of the requiredfield validator control as the value at the zero index of the drop downlist; in this case we will set the value to Select.
     
  2. Range Validator

    It is used for validating the numeric fields such as the age of the user or any numeric value that has a minimum value or maximum value. Properties to be set are:

    • ControlToValidate: the name of the target control whose value needs to be checked to determine if it has a value or not.
    • ErrorMessage: Error Message to be displayed if validation fails.
    • Text: Text to be displayed if the target control does not hold any value.
    • Minimumvalue: It is the minimum value that the target control can hold.
    • MaximumValue: It is the maximum value that the target control can hold.
     
  3. RegularExpressionValidator

    It makes use of a builtin regex expression or we can also specify custom regex expression. It is usually used for validating emails, phone numbers or any pattern-specific validation.

    Properties

    • ControlToValidate: the name of the target control whose value is to be checked to determine whether it has a value or not.
    • ErrorMessage: Error Message to be displayed if validation fails.
    • Text: Text to be displayed if the target control does not hold a value.
    • ValidationExpression: Here we specify our required regex expression to validate a particular pattern.
     
  4. CompareValidator

    It is used for performing validation when the inputs of two fields are to be compared, for example word and Confirmword.

    Properties

    • ControlToValidate: the name of the target control whose value is to be checked to determine whether it has a value or not.
    • ControlToCompare: the name of the control whose input needs to be matched with the current control.
    • ErrorMessage: Error Message to be displayed if validation fails.
    • Text: Text to be displayed if the target control does not hold any value.
     
  5. ValidationSummary

    This control is used for displaying the errors on the page in a list or paragraph format in the form of a summary.

The following is the output of our demo.

Image 1.jpg

Hope you liked the example and that it may help you in your project.


Similar Articles