Web
Analytics
Data Annotation and Validations in ASP.NET MVC 5 | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Data annotations are used to perform client side and server side validations on a page.

It’s a mechanism, Atrrributeswe can use it to feed metadata into application and framework will display and render HTML markup.
Data Annotations are Attributes which are used to perform server-side validations as well as client side validations.
Data Attributes with Example.
1. Required
[Required]
public string UserName{get;set;}
2. StringLength
StringLength(20)
Public string Name{get;set;}
3. RegularExpression
RegularExpression(“.+@.+\\..+”,ErrorMessage=“Email format is not good”)
public string Email{get;set;}
4. Range
[Range(30,35)
Public int Age{get;set;}
5.Compare
Public string password{get;set;}
[Compare(“Password”,ErrorMessage=“Password and Compare Password should be same”)
Public string ConfirmPassword{get;set;}
For Source code and other tutorial Please visit
www.yogeshdotnet.com

using DataAnnotationHindi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DataAnnotationHindi.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(Employee emp)
        {

            if (!string.IsNullOrEmpty(emp.username))
            {
                
                return RedirectToAction("Message");
            }
            else
            {
                ModelState.AddModelError("username", "UserName is Required");
                return View();
            }
        }
        public ActionResult Message()
        {
            return View();
        }
    }
}
@model DataAnnotationHindi.Models.Employee
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm())
{
    <p>
        @Html.TextBoxFor(m => m.username)
        @Html.ValidationMessageFor(m => m.username)
    </p>
    <input type="submit" name="btn" value="Save data" />

}


Server Side Validation using Data Annotations

using DataAnnotationHindi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DataAnnotationHindi.Controllers
{ 
@model DataAnnotationHindi.Models.Employee
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm())
{
    <p>
        @Html.LabelFor(m=>m.username)
        @Html.TextBoxFor(m => m.username)
        @Html.ValidationMessageFor(m => m.username)
    </p>
    <p>
        @Html.LabelFor(m => m.Email)
        @Html.TextBoxFor(m => m.Email)
        @Html.ValidationMessageFor(m => m.Email)
    </p>
    <p>
        @Html.LabelFor(m => m.Age)
        @Html.TextBoxFor(m => m.Age)
        @Html.ValidationMessageFor(m => m.Age)
    </p>
    <p>
        @Html.LabelFor(m => m.Password)
        @Html.PasswordFor(m => m.Password)
        @Html.ValidationMessageFor(m => m.Password)
    </p>
    <p>
        @Html.LabelFor(m => m.RePassword)
        @Html.PasswordFor(m => m.RePassword)
        @Html.ValidationMessageFor(m => m.RePassword)
    </p>
    <p>
       
        @Html.CheckBoxFor(m => m.Terms) Please select terms and conditions
        @Html.ValidationMessageFor(m => m.Terms)
    </p>
    <input type="submit" name="btn" value="Save data" />

}


    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(Employee emp)
        {
            if(ModelState.IsValid)
            {
                return RedirectToAction("Message");
            }
            return View();
          
        }
        public ActionResult Message()
        {
            return View();
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace DataAnnotationHindi.Models
{
    public class Employee
    {
        [Required(ErrorMessage ="Please Enter User Name")]
        [StringLength(30)]
        [Display(Name ="Enter User Name")]
        public string username { get; set; }

        [Required]
            [RegularExpression(".+@.+\\..+", ErrorMessage="Email format is not good")]
        [Display(Name = "Enter Email")]
        public string Email { get; set; }

        [Range(20,35)]
        [Required]
        public int Age { get; set; }
        [Required]
        public string Password { get; set; }
        [Required]
        [Compare("Password", ErrorMessage ="Password and Retype password must be same")]
        public string RePassword { get; set; }
       
        [validatecheckbox(ErrorMessage ="Please check Terms and Conditions")]
        public bool Terms { get; set; }

        public class validatecheckbox: RequiredAttribute
        {
            public override bool IsValid(object value)
            {
                return (bool)value;
            }
        }
    }
}

 

 

Download

Sending
User Review
0 (0 votes)