Introduction
Validation is one of the primary concepts when we develop web sites. For any website, there are many input fields. When users enter the data in those input fields, the input fields are exposed to the client browser. Users can enter anything they want into the input fields. If the user enters some wrong data, then we will have some irrelevant or wrong data in the database also. Providing the authority for the user to enter data in input fields which will then be inserted into the database and used for many purposes may cause security holes in the system if we don't put any restrictions on the input fields.
Put more simply, we can say that validations are some rules set by the developer on input fields that satisfy the business rules for that particular input field in order to have the proper data in the system.
There are two types of validations:
1. Server-Side Validations
2. Client-Side Validations
While performing validations we need to take care of not only the proper validation, but also ensure the validation meets the business rule as per the requirement. From a security perspective, it is often possible that hackers may bypass client-side validation and insert vulnerable data into the server.
We will see the validations in MVC. Validations can be performed using the below methods:
Using ModelState Object
Using Data Annotation
Using Jquery
Custom validations can be applied to input fields using classes of MVC framework.
Server-Side Validations
Validations can be performed on the server side or on the client side ( web browser). User input validation that takes place on the Server Side during a postback session is called Server-Side Validation. User input validation that takes place on the Client Side (web browser) is called Client-Side Validation.
Client-Side Validations
ASP.NET MVC client-side validation is based on the jQuery validation plugin. It can be said that MVC's client-side validation is an opinionated version of how jQuery validation should work in an ASP.NET MVC project. Despite this, the underlying implementation is fully based on jQuery's.
In the next section, we will see how to perform validation along with an example. I hope this information will help you and increase your knowledge.