top of page
Writer's pictureThe Tech Platform

How to perform validations in ASP .NET web applications

For any product that you manufacture today, it is important to perform validation before delivering it to the client. You need to compulsorily verify if all the required parameters are met by the manufactured product. In the present world, it is possible to do most of these validations over the internet.

Consider a hypothetical scenario where you are booking a flight ticket. While booking you will be asked to fill in your personal details (most of which will be text fields). Now imagine what will happen if no validations are present. It will increase the number of invalid data getting recorded for many travelers. To avoid this situation, it is important to have validations for all the input fields in websites.

As a developer, to make a field mandatory, you need to write the same code logic every time. Instead, ASP.Net comes up with the idea of validation in the form of Data Annotation. It provides a set of predefined generic controls that can be used with the fields that require validations. All that developers need to do is provide some specification for the validators and consume it as you go in the application.

Let’s see in detail about the validators that come along with ASP. Net and how to implement them in a project.

Introduction to ASP.Net Validations

ASP.Net Validations provides a handy mechanism to validate an input without the necessity of writing new pieces of code for it. Also, it offers custom validators to have user-defined validation for the fields. Users can even add validation to any control, which we will see in detail in the next section. Validation of the controls is done when the controls’ focus changes or when the submit button is clicked. At that instance, the provided data annotations validate the input fields and set the page to an invalid or valid state based on the outcome. These validations happen on the server-side of the code.

Similarly, it is possible to have multiple validations for a single field. For example, you can have a required field validator as well as a range validator to check if the number lies within the provided range. Let’s see in detail the different types of available validation and their implementation in the next section.

Practical implementation of validation controls

Before we jump into the implementation part, here are the list of available validations in ASP. Net.

  1. Required Field Validator,

  2. Compare Validator,

  3. RangeValidator,

  4. RegularExpression Validator,

  5. Custom Validator

For implementation, we are going to create a Sign-Up page and explore the above-mentioned validators on the input fields that are present on the page.

Step 1 Create an ASP.Net Core Web application from the project template and provide a valid name to the project.


Step 2 Choose the template as WebApplication (Model-View-Controller) and create the project.

Step 3 The folder structure of the project will look like below.

Step 4 Create a Model which holds the data for the SignUp form. To create a model, right-click on the Models folder, and select the Add option. Choose Class and provide a valid name for the class and create the properties that are going to be part of the SignUp page.

Step 5 Once the model is created, right-click on the home folder under views, and click on add Razor Page. And then provide the SignUp Model as the Model class and choose the template of the view as create.

Depending on the properties in the Model, the fields for the UI have been created.

Step 6 Now, to make the sign-up page our home page, go to the startup file. Under Configure, modify the existing configuration as mentioned in the below image (action=SignUp).

And change the Index method in HomeController to SignUp.

Step 7 Let’s begin implementing the validations on the model. As a first step, let’s implement the required field validator. To implement it, add the *[Required]* attribute above each property in the SignUp model field. The System.ComponentModel.DataAnnotations has to be imported in the header section.

Step 8 Now, run the application. The UI will open up in the provided browser. Do you see all the fields in the UI which are present on the Model page as well?

Now, if we try to submit the page without filling the details, it will throw errors in all the fields. As we have marked all the fields as required.

Step 9 Now, let’s try to add up some more validations. Suppose we want the username to be a minimum of 10 characters and a maximum of 50 characters, and also the age should be between 1 to100 (Range Validator), then to implement these, the validation attributes in the below image have to be added.

And the error looks like this.

Step 10 As we have a password and confirm password fields, the input in both of them has to be the same. Let’s see how to implement the compare validator for the password fields. Provide the name of the property to compare within the bracket of Compare attribute.

And the error looks like this.

Step 11 Let’s include some regular expression validators for the username field. Let us validate whether the text entered in the username field is of an email id format. To implement it, add the attribute RegularExpression and provide the expression with the brackets. Also, for all the validators, it is possible to provide a custom error message which will override the default one.

And the error looks like this.

Step 12 To implement a custom validator, create a class and inherit ValidationAttribute. Now in the class override, the method IsValid returns bool type. Also, add the required conditions that need to be validated.

Add the created class name as the shorthand to the corresponding property.


Conclusion

ASP.Net by default provides very useful validators which come in handy for validating the fields. These features save a lot of code maintainability for validating the fields and also the time taken for implementing the changes.



Source: Medium - ParTech


The Tech Platform

0 comments

Commenti


bottom of page