Form Validation in ASP.NET MVC 5 with Example

In this chapter, you will learn:
1. What is Validation and why it needed?
2. Server Side Validation with ModelState Object.

What is Validation and why it needed?

Validation means examine user’ input-data to check whether,

i. Is it in the correct format or not?
ii. Is it Empty?
iii. Does it contain malicious code?
iv. Is it in correct range, etc

As a programmer, the first rule you know that never trust on user’s input. A user is free to input anything in the input field and it is a programmer’s responsibility to force them to enter correct value in a field. Validation ensures the correct input submission in the database and enhances user experience. It saves from unnecessary round trip and exception messages. In ASP.NET MVC 5, you can apply the following method to validate input field.

  1. Validation using ModelState Object (Server side validation)
  2. Validation using Data Annotation (Client-Side Validation)
  3. Validation using JavaScript and JQuery
  4. Custom Validation on the input field.

Once, you will learn all these 4 validation techniques you will be able to validate any input field in MVC. I have created a very basic form to teach you how validation works. However, later you will learn to validate each input field and value, like validating age, email, mobile no. and other value.

Let’s start learning Validation with programming example.


Share your thought