---
title: "What is Fluent Validation in MVC ?"  
description: "What is Fluent Validation in MVC ?"  
author: "Anonymous User"  
published: 2018-10-08  
updated: 2018-10-16  
canonical: https://www.mindstick.com/forum/34681/what-is-fluent-validation-in-mvc  
category: "c#"  
tags: ["asp.net mvc", "mvc", "validation"]  
reading_time: 3 minutes  

---

# What is Fluent Validation in MVC ?

Fluent [Validation in MVC](https://www.mindstick.com/forum/34733/how-can-use-validation-in-mvc)

## Replies

### Reply by Anonymous User

Thanks, Sanat Sir

Your answer is very helpful.

### Reply by Anonymous User

**"Fluent [validation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) in [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc)"...**

Often the fluent validation is a validation class library for the .NET framework and it uses lambda expressions for building validation rules for your business objects. If you want to use general validation in asp.net MVC application then data annotations validation is good but in case if you want to implement complex validation then you need to use Fluent Validation.

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentValidation;
namespace MyFirstAppMVC.Models
{
    public class EmpValidator : AbstractValidator<Emp>
    {

        public EmpValidator()
        {

            RuleFor(x => x.EmpName).NotEmpty().WithMessage("EmpName is required");
            RuleFor(x => x.EmpDateOfBirth).NotEmpty().WithMessage("EmpDateOfBirth is required");
            RuleFor(x => x.EmpDateOfBirth).Must(AgeValidate).WithMessage("Invalid date Emp age must be 18 or greater than 18");
            RuleFor(x => x.EmpEmailID).NotEmpty().WithMessage("Emp EmailID is required");

            RuleFor(x => x.EmpEmailID).EmailAddress().WithMessage("Emp EmailID is required");
            RuleFor(x => x.EmpAddress).NotEmpty().WithMessage("EmpAddress is required");
            RuleFor(x => x.EmpAddress).Length(20, 250).WithMessage("Address must be between 20 to 250 words");
            RuleFor(x => x.Password).NotEmpty().WithMessage("Password is required");
            RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("ConfirmPassword is required");
            RuleFor(x => x.ConfirmPassword).Equal(x => x.Password).WithMessage("Password do not Match");

        }
        private bool AgeValidate(DateTime value)
        {
            DateTime now = DateTime.Today;
            int age = now.Year - Convert.ToDateTime(value).Year;
            if (age < 18)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
```

```
public EmpValidator()

{

RuleFor(x => x.EmpName).NotEmpty().WithMessage("Student Name is required");

RuleFor(x => x.EmpDateOfBirth).NotEmpty().WithMessage("EmpDateOfBirth is required");

RuleFor(x => x.EmpEmailID).NotEmpty().WithMessage("Emp EmailID is required");

RuleFor(x => x.EmpAddress).NotEmpty().WithMessage("Emp Address is required");

RuleFor(x => x.Password).NotEmpty().WithMessage("Password is required");

RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("Confirm Password is required");

}
```

## Age Validation Method

```
private bool AgeValidate(DateTime value){DateTime now = DateTime.Today;int age = now.Year - Convert.ToDateTime(value).Year;if (age < 18){return false;}else{return true;}}
```

## A rule for Date of Birth

```
 RuleFor(x => x.StudentDOB).Must(AgeValidate).WithMessage("Invalid date student age must be 18 or greater than 18");   
```

## Validating EmailID (Email Address)

```
RuleFor(x => x.StudentEmailID).EmailAddress().WithMessage("Student EmailID is required");
```

Etc...

All the examples above are related to **"Fluent validation ".**


---

Original Source: https://www.mindstick.com/forum/34681/what-is-fluent-validation-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
