Mvc4 Validation using Razor View in asp.net

Mvc4 Validation using Razor View in asp.net

Introduction:  This tutorial explain how to validation can display in mvc4 like Phone number,Email,conform password, range validation ,required field validation etc..simply validation can perform using javascript or Jquery but here in mvc4 validation can perform in Model class  with data structure as shown in below.

here need to add following namespace.

 using System.ComponentModel.DataAnnotations;


validation:
   
        public int id { get; set; }

        [Required(ErrorMessage="Name is required ?")]
        public string name { get; set; }

        [Required(ErrorMessage="Password Required ?")]
        [StringLength(50,ErrorMessage="password are 10 char ?",MinimumLength=7)]
        [Display(Name="password")]
        [DataType(DataType.Password)]
        public string password { get; set; }

        [Required(ErrorMessage = "Password Required ?")]
        [StringLength(50, ErrorMessage = "password  10 char", MinimumLength = 7)]
        [Compare("password",ErrorMessage="pasword not match try again ?")]
        public string cpassword { get; set; }

        public string gender { get; set; }
        public string subject { get; set; }

        [Required(ErrorMessage = "Nomber is required ?")]
        [StringLength(50, ErrorMessage = "password r 10 char", MinimumLength = 10)]
        public string  number { get; set; }

        [Required(ErrorMessage = "Please Enter Email Address")]
        [RegularExpression(".+@.+\\..+", ErrorMessage = "Please Enter Correct Email Addres")]
        public string email  { get; set; }

        public string city { get; set; }

Output can looke like following.


Previous
Next Post »