mvc3 client side validation example

Ravikumar
0

mvc3 client side validation example:

Here we are covering mvc3 client side validation .we are going to make validations like compare validation and string length ,required field,and email validation etc.

so let's Start.....

Create new mvc3 project 
Create empty project

You will see like this

Now Rightclick on Models in solution explorer and add one Register.cs class file



Type Below code in Register.cs file in Register Class
  Here you Must have to Enter two Name space using System.ComponentModel.DataAnnotations; for require field validation and using System.Web.Mvc; for compare Validation


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace MvcValidations.Models
    {
    public class Register
        {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [RegularExpression(@"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$",ErrorMessage="Please EnterValid Email ID")]
        [Display(Name = "Email address")]
        public string Email { get; set; }

        [Required]
        [StringLength(50,ErrorMessage="Enter up to 50character")]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation do not match.")]
        public string ConfirmPassword { get; set; }
        }
    }
 Here you can use email regular Expression
Now Right click on Controllers Folder and add new controller (RegisterController.cs) like this


 Select Empty Controller

Now type below code in RegisterController.cs 

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

namespace MvcValidations.Controllers
    {
    public class RegisterController : Controller
        {
        //
        // GET: /Register/

        public ActionResult Index()
            {
            return View();
            }
        [HttpPost]
        public ActionResult Index(Register reg)
            {
            return View();
            }
        }
    }
  

Now Right click on Index and add view


Here you have to Build solution .if you not buid solution you could not find class name in Model Class Drop down
Select Class name

Select Scaffold template Create

Now go to Global.asax.cs nad change given place  

using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcValidations
    {
    // Note: For instructions on enabling IIS6 or IIS7 classic mode,
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
        {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
            {
            filters.Add(new HandleErrorAttribute());
            }

        public static void RegisterRoutes(RouteCollection routes)
            {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Register", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

            }

        protected void Application_Start()
            {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            }
        }
    }

Now Run this application

 
  


 


 I hope This Program will help You .If You have any Query Then type.I will try to solve Your Query.

-Ravikumhar

Source will be Available Soon.



Tags:

Post a Comment

0Comments

Post a Comment (0)