Custom Validation Attribute in ASP.Net MVC 4
In this
blog, I’m explaining how to create the custom validation attribute in asp.net
mcv 4 and how to use it in our application.
Step 1
First
create an empty asp.net mvc 4 application and add a model class named “Person”
in it:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace CustomValidationMvcApp.Models
{
public class Person
{
public int Id { get; set; }
[Required(ErrorMessage="Name is madatory")]
public string Name { get; set; }
[Required(ErrorMessage="Date Of Birth is mandatoy")]
[AgeComparison]
[Display(Name="Date Of Birth")]
public DateTime DOB { get; set; }
}
}
Step 2
Now create another class
named “AgeComparison”
and this class will have the business logic of our custom attribute
AgeComparison:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace CustomValidationMvcApp.Models
{
public class AgeComparison : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext
validationContext)
{
DateTime date = Convert.ToDateTime(value);
long ticks = DateTime.Now.Ticks
- date.Ticks;
DateTime dtAge = new DateTime(ticks);
if (dtAge.Year <= 18)
{
return new ValidationResult("You
are not eligible for vote");
}
else{
return new ValidationResult("You
are eligible for vote");
}
}
}
}
This custom attribute will tell you
that you are eligible for vote or not by comparing your date of birth.
Step 3
Now add a controller to
the project named “HomeController” and write the below code in it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CustomValidationMvcApp.Models;
namespace CustomValidationMvcApp.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Person model)
{
return View();
}
}
}
Step 4
Now add a view named “Index”
and write the below code in it:
@model CustomValidationMvcApp.Models.Person
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div style="margin: 0 auto; width:600px;">
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<div style="margin-top:10px;">
<div style="width:100px;display:inline-block">
@Html.LabelFor(x => x.Name)
</div>
<div style="width:200px;display:inline">
@Html.TextBoxFor(x => x.Name)
</div>
<div style="width:200px;display:inline">
@Html.ValidationMessageFor(x =>
x.Name)
</div>
</div>
<div style="margin-top:10px;">
<div style="width:100px;display:inline-block">
@Html.LabelFor(x => x.DOB)
</div>
<div style="width:200px;display:inline">
@Html.TextBoxFor(x => x.DOB)
</div>
<div style="width:200px;display:inline">
@Html.ValidationMessageFor(x =>
x.DOB)
</div>
</div>
<div style="margin-top:10px;">
<div>
</div>
<div >
<input type="submit" value="Submit"/>
</div>
</div>
}
</div>
</body>
</html>
Your solution explorer
will look like this:
OutPut
Now run the application:
No comments:
Post a Comment