How do Validation Controls in ASP.Net work?

How do Validation Controls in ASP.Net work?

Validation Controls are those controls that validate the data entered by the user online.

ASP.Net supports the use of 3 different types of controls.

  1. HTML Controls
  2. Web Form Controls
  3. Validation Controls

Currently, almost every website online has a section where the user has to enter some input, like; registration forms, log-in forms, or etc. This data entered by the user need to be checked and controlled as any sort of data (invalid data) should not be submitted to the database or server. A developer can easily control this data entered by a user by making use of the Validation Controls.

ASP.Net validation controls are used to prevent the user from entering malicious data or invalid data to the website/ server. For example; a website asks the user to enter his e-mail in a box whereas the user enters his telephone number there.

Validation can be of 2 types; Client side validation and Server side validation. In client side validation the browser itself checks for the data entered by the user to be valid or not. However; in server side validation the validation of data is on the server itself. Commonly, client side validation is preferred to be used more often. As it takes less time to be executed.

Validation Controls Types



ASP.Net supports the following different types of Validation Controls.

  1. Required Field Validation
  2. Compare Validation
  3. Range Validation
  4. Regular Expression Validation
  5. Custom Validation
  6. Summary Validation

Every single Validation Control mentioned above has their own properties. But some of the properties which are commonly used by all of them are as;

  • ControlToValidate: It specifies which control the validator is assigned to.
  • ErrorMessage: It is the message which is to be shown to the user if the user has not entered the valid/ expected value.
  • Text: It is the same as the “ErrorMessage” but this property has a precedence.
  • Display: It specifies how the “ErrorMessage” or “Text” should be displayed. It can take the values (None, Static, Dynamic)
  • ForeColor: It specifies the color of the “ErrorMessage” or “Text”

And etc. The properties of the validation controls can be modified both form the property window and from the source code window itself.

Required Field Validation



Required Field Validation Control is the simplest validation control in ASP.Net. It is used when an information box/ field is necessary for the user to be filled. For example; you want to fill an online registration form. The developer makes the name field to be compulsory for every single user to be filled. Here he can make use of a Required Field Validator Control. If the user does not enter his name, an error message or text is shown beside the input box.

Example Source Code:

As you can see above, the RequiredFieldValidator is assigned to the TextBox “txtName”. So the TextBox becomes compulsory for the user to fill.

Read: Working with Multiple Forms in Visual Basic. Net

Compare Validation

Compare Validation Control is used to compare 2 values entered by the user, or compare a value entered by the user with a static value, or compare the Data Type of a value entered by the user.

Example Source Code:

Another Example Source Code:

Specific properties of Compare Validation Control are:

  • ControlToCompare: It specifies which value/ input field has to be compared. For example, in “Password” and “Confirm Password” the second one has to be compared to the first one.
  • Operator: It is the type of comparison that has to be done. Whether you want 2 values to be “equal” or “Less than” or even want to compare the data type “DataTypeCheck”.
  • Type: It emphasizes the type of the data to be compared. It can be string, integer, double, date or currency.

Range Validation



This validation control is used to specify 2 values between which the user can enter/ fill the data. For example; while filling your percentage you can enter a value between 40 to 100. As below 40 you are fail and above 100 no percentage exists. So you can make use of Range Validation Control.

Example Source Code:

Specific properties of Range Validation Control are:

  • MaximumValue: It is the max value that can be entered.
  • MinimumValue: It is the min value that can be entered.

Read: Bubble Sort Algorithm, Code & Explanation

Regular Expression Validation

Regular Expression Validation Control is a powerful tool to check if a value input by the user is according to a specific pattern set before hand or not. It is mostly used to check the validation of expressions like E-mail addresses, phone numbers, digit/zip codes and so on.

Example Source Code:

Every Regular Expression Validation Control has a property called “ValidationExpression”. It is a specific code which is used to check the format of the value entered by the user and the standard format. E-mail address, phone number, and zip code has each their own specific ValidationExpression.

Validation Summary (Summary Validation)

Validation Summary is a control that checks for all the validation controls in the code and summarizes all of them and reports back. If there are error messages, it will list all of them and will display to the user.

Validation Summary control does not hold any specific property. But you can modify the format (display mode) that it will show the error messages from “List” and “Single Paragraph”.

Example Source Code:

Custom Validation

This Validation Control is a user-defined validation control. Based on certain criteria the developer can develop his own validation control. For example, you will specify that the password entered by the user should be at least 8 characters and should include at least one number and one capital letter. You can specify this criteria using a user defined validation controls.


Full Program Source Code

ASPX Source Code:

ASPX.VB Code (Code behind the Submit Button):

Output:

Validation Controls Output

Validation Controls Output

Validation Controls Output

Important

It is necessary to know that one validation controls can only be assigned to one input control such as an input box or text box. In other words, for every single input box or text box that you want to put validation on, a separate validation controls should be defined. However; more than one validation controls can also be assigned to one control.


Previous Bubble Sort Algorithm, Code and Explaination
Next How to make use of Namespaces in the .Net Framework?

About author

Bahonar Javid
Bahonar Javid 43 posts

<p>* Computer Application Student<br /> * Co-Founder of www.codejow.com<br /> * Love to code and Learn new languages<br /> * Enjoy playing football</p>

View all posts by this author →

You might also like