Posts

Showing posts from February, 2014

Custom Validation in ASP.Net MVC 4

To implement custom validation in C# ASP.NET MVC4, it needs to inherit “ValidationAttribute” class and overwrite “IsValid” method in your class. For this you need to add namespace System.ComponentModel.DataAnnotations. Here, I am creating a class for my custom password attributes and its minimum length should be of 5 chars and maximum   length should be of 10 chars. I have initialized the values in its constructor and defined the code logic in overridden method “IsValid”. public class PasswordAttribute : ValidationAttribute {             int MinLength;             int MaxLength; public PasswordAttribute( int minLength, int maxLength)         {             this .MinLength = minLength;             this .MaxLength = maxLength;         }             public override bool IsValid( object value)        {             // TODO:                      if (condition_satisfied)                 return true ;             else