Data Annotations
Validation in MVC can be done using Data Annotations that are applied to both the client and server side.
Data Annotation attributes are used to validate the user inputs when posting the form. All the Data Annotation attributes like Required, Range are derived from the ValidationAttribute class that is an abstract class. The ValidationAttribute base class lives in the System.ComponentModel.DataAnnotations namespace.
This article is showing how to create a custom Data Annotations step by step:
Image 3
Image 4
Validation in MVC can be done using Data Annotations that are applied to both the client and server side.
Data Annotation attributes are used to validate the user inputs when posting the form. All the Data Annotation attributes like Required, Range are derived from the ValidationAttribute class that is an abstract class. The ValidationAttribute base class lives in the System.ComponentModel.DataAnnotations namespace.
This article is showing how to create a custom Data Annotations step by step:
- Step 1
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel.DataAnnotations;
- using System.Text.RegularExpressions;
- namespace Custom_DataAnnotation_Attribute.Models
- {
- public class CustomEmailValidator : ValidationAttribute
- {
- protected override ValidationResult IsValid(object value, ValidationContext validationContext)
- {
- if (value != null)
- {
- string email = value.ToString();
- if (Regex.IsMatch(email, @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", RegexOptions.IgnoreCase))
- {
- return ValidationResult.Success;
- }
- else
- {
- return new ValidationResult("Please Enter a Valid Email.");
- }
- }
- else
- {
- return new ValidationResult("" + validationContext.DisplayName + " is required");
- }
- }
Create a New MVC 4 Application.
Step 2
Select Razor View Engine.
Step 3
Here I am showing Email validation on the Employee Registration Form.
Select the Model Folder then select Add New Item -> Add New Class CustomEmailValidator.cs.
Here we need to inherit the ValidationAttribute and need to override the IsValid method.
CustomEmailValidator.cs
- Right-click on the Model Folder then select Add New Item -> Add New Class -> EmployeeModel.cs.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel.DataAnnotations;
- namespace Custom_DataAnnotation_Attribute.Models
- {
- public class EmployeeModel
- {
- public string Name { get; set; }
- [CustomEmailValidator]
- public string Email { get; set; }
- public string Password { get; set; }
- public string Mobile { get; set; }
- }
- }
Step 5
- Now right-click on the Controller Folder then select Add New Controller.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Custom_DataAnnotation_Attribute.Models;
- namespace Custom_DataAnnotation_Attribute.Controllers
- {
- public class ManageEmployeeController : Controller
- {
- //
- // GET: /ManageEmployee/
- public ActionResult Index()
- {
- return View();
- }
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult Index(EmployeeModel model)
- {
- if (ModelState.IsValid)
- {
- ViewBag.Name = model.Name;
- ViewBag.Email = model.Email;
- ViewBag.Password = model.Password;
- ViewBag.Mobile = model.Mobile;
- }
- return View(model);
- }
- public ActionResult Register()
- {
- return View();
- }
- }
- }
Image 1
Here in ManageEmployeeController I added a new Index Action.
- Now right-click on the Index Action then select Add View.
Image 2
Step 7
- Now the ManageEmployee's index.cshtml file will look like:
- @model Custom_DataAnnotation_Attribute.Models.EmployeeModel
- @{
- ViewBag.Title = "Register Employee";
- }
- <h2>Register</h2>
- @using (Html.BeginForm()) {
- @Html.ValidationSummary(true)
- <fieldset>
- <legend>EmployeeModel</legend>
- <div class="editor-label">
- @Html.LabelFor(model => model.Name)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Name)
- @Html.ValidationMessageFor(model => model.Name)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Email)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Email)
- @Html.ValidationMessageFor(model => model.Email)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Password)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Password)
- @Html.ValidationMessageFor(model => model.Password)
- </div>
- <div class="editor-label">
- @Html.LabelFor(model => model.Mobile)
- </div>
- <div class="editor-field">
- @Html.EditorFor(model => model.Mobile)
- @Html.ValidationMessageFor(model => model.Mobile)
- </div>
- <p>
- <input type="submit" value="Create" />
- </p>
- </fieldset>
- }
- <div>
- @Html.ActionLink("Back to List", "Index")
- </div>
- @section Scripts {
- @Scripts.Render("~/bundles/jqueryval")
- }
Image 3
Image 4
No comments:
Post a Comment