Validation in the Asp.Net MVC
Controller:
public ActionResult chkvalidation()
{
return View();
}
[HttpPost]
public ActionResult chkvalidation(EmpDet empdet)
{
if (ModelState.IsValid)
{
//If the validation succeed,Execute the logic in the if condition
}
return View();
}
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace validation.Models
{
public class EmpDet
{
[Required]
[Display(Name="Employee Id")]
public int Empid { get; set; }
[Display(Name="Employee Name")]
[Required(ErrorMessage = "Employee Name is required")]
[StringLength(100,MinimumLength = 3)]
public string EmpName { get; set; }
[Required(ErrorMessage = "Employee Address is required")]
[StringLength(5)]
public string Address { get; set; }
[Required(ErrorMessage = "Salary is required")]
[Range(3000, 10000000, ErrorMessage = "Salary must be between 3000 and 10000000")]
public int Salary { get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
[MaxLength(50)]
[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]
public string Email { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime JoiningDate { get; set; }
}
}
View:
@model validation.Models.EmpDet
@{
ViewBag.Title = "chkvalidation";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<h2>chkvalidation</h2>
@Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.")
@using(@Html.BeginForm("chkvalidation","Home",FormMethod.Post))
{
<fieldset>
<legend>EmpDet</legend>
<ol>
<li>
@Html.LabelFor(m => m.Empid)
@Html.TextBoxFor(m => m.Empid)
@Html.ValidationMessageFor(m => m.Empid)
</li>
<li>
@Html.LabelFor(m => m.EmpName)
@Html.TextBoxFor(m => m.EmpName)
@Html.ValidationMessageFor(m => m.EmpName)
</li>
<li>
@Html.LabelFor(m => m.Address)
@Html.TextBoxFor(m => m.Address)
@Html.ValidationMessageFor(m => m.Address)
</li>
<li>
@Html.LabelFor(m => m.Salary)
@Html.TextBoxFor(m => m.Salary)
@Html.ValidationMessageFor(m => m.Salary)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)
</li>
<li>
@Html.LabelFor(m => m.JoiningDate)
@Html.TextBoxFor(m => m.JoiningDate)
@Html.ValidationMessageFor(m => m.JoiningDate)
</li>
</ol>
<input type="submit" value="Submit" />
@*
<div class="display-label">
@Html.DisplayNameFor(model => model.Empid)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Empid)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.EmpName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.EmpName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Address)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Address)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Salary)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Salary)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Email)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Email)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.JoiningDate)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.JoiningDate)
</div>*@
</fieldset>
}
<p>
@Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) |
@Html.ActionLink("Back to List", "Index")
</p>
Screenshot:
Empty Validation:
Check the mandatory field


Controller:
public ActionResult chkvalidation()
{
return View();
}
[HttpPost]
public ActionResult chkvalidation(EmpDet empdet)
{
if (ModelState.IsValid)
{
//If the validation succeed,Execute the logic in the if condition
}
return View();
}
Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace validation.Models
{
public class EmpDet
{
[Required]
[Display(Name="Employee Id")]
public int Empid { get; set; }
[Display(Name="Employee Name")]
[Required(ErrorMessage = "Employee Name is required")]
[StringLength(100,MinimumLength = 3)]
public string EmpName { get; set; }
[Required(ErrorMessage = "Employee Address is required")]
[StringLength(5)]
public string Address { get; set; }
[Required(ErrorMessage = "Salary is required")]
[Range(3000, 10000000, ErrorMessage = "Salary must be between 3000 and 10000000")]
public int Salary { get; set; }
[Required(ErrorMessage = "Please enter your email address")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
[MaxLength(50)]
[RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]
public string Email { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime JoiningDate { get; set; }
}
}
View:
@model validation.Models.EmpDet
@{
ViewBag.Title = "chkvalidation";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<h2>chkvalidation</h2>
@Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.")
@using(@Html.BeginForm("chkvalidation","Home",FormMethod.Post))
{
<fieldset>
<legend>EmpDet</legend>
<ol>
<li>
@Html.LabelFor(m => m.Empid)
@Html.TextBoxFor(m => m.Empid)
@Html.ValidationMessageFor(m => m.Empid)
</li>
<li>
@Html.LabelFor(m => m.EmpName)
@Html.TextBoxFor(m => m.EmpName)
@Html.ValidationMessageFor(m => m.EmpName)
</li>
<li>
@Html.LabelFor(m => m.Address)
@Html.TextBoxFor(m => m.Address)
@Html.ValidationMessageFor(m => m.Address)
</li>
<li>
@Html.LabelFor(m => m.Salary)
@Html.TextBoxFor(m => m.Salary)
@Html.ValidationMessageFor(m => m.Salary)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)
</li>
<li>
@Html.LabelFor(m => m.JoiningDate)
@Html.TextBoxFor(m => m.JoiningDate)
@Html.ValidationMessageFor(m => m.JoiningDate)
</li>
</ol>
<input type="submit" value="Submit" />
@*
<div class="display-label">
@Html.DisplayNameFor(model => model.Empid)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Empid)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.EmpName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.EmpName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Address)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Address)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Salary)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Salary)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.Email)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Email)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.JoiningDate)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.JoiningDate)
</div>*@
</fieldset>
}
<p>
@Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) |
@Html.ActionLink("Back to List", "Index")
</p>
Screenshot:
Empty Validation:
Check the mandatory field