Sunday, October 14, 2012

Required and compare Validations in MVC4

I have created a registration form  for my application,then placed some validation for user inputs based on requirement.In we have to use validation controls to implement validations.In Asp.Net MVC System.ComponentModel.DataAnnotations name space has been included to get the validation class to perform validations on it.The below code has done for required validation for user name and compare validation for password field

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace TestApp.Models
public class RegisterModel
public int Id { get; set; }
public string UserName { get; set; }
[StringLength(100,ErrorMessage="Password must have 5 characters at least",MinimumLength=5)]
public string Password { get; set; }
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }

There is one more validation  i have done i:e String-length validation on user name.This can make the password minimum length to "5"
Post a Comment