Quantcast
Channel: amitpatelit » MVC
Viewing all articles
Browse latest Browse all 11

Custom validation in MVC application

$
0
0

In MVC for adding a validation there is need to set attributes and there are few inbuilt validation attributes related classes are available and based on that we can set validation.

But in practical scenario there are another couple of validation needs to required implement with our customer logic and also important part is that add message from resource file to handle multilingual support.

In this blog I have created below custom validation with code example.

  1. Email validation
  2. Numeric validation
  3. Numeric validation with decimal support
  4. Future Date Validation

Below are the class we have created for this validation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespaceBusiness.Model.ValidationAttributes
{
public class EmailValidationAttribute : RegularExpressionAttribute
{
public EmailValidationAttribute()
: base(@"^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-zA-Z0-9]{1}[a-zA-Z0-9\-]{0,62}[a-zA-Z0-9]{1})|[a-zA-Z])\.)+[a-zA-Z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$")
{
}
}
public class NumbericValidationAttribute : RegularExpressionAttribute
{
public NumbericValidationAttribute()
: base(@"^-[0-9]+$|^[0-9]+$")
{
}
}
public class NumbericDecimalValidationAttribute : RegularExpressionAttribute
{
public NumbericDecimalValidationAttribute()
: base(@"^[.][0-9]+$|[0-9]*[.]*[0-9]+$")
{
}
}
public class FutueRangeValidationAttribute : ValidationAttribute
{
private DateTime? MinDate;
public DateRangeValidationAttribute()
: base()
{
this.MinDate = DateTime.Now;
}
public override bool IsValid(object value)
{
DateTime dtValue = Convert.ToDateTime(value);
return dtValue > this.MinDate;
}
}
}

Below are the example of usage of this validation attributes.

In model need to add this validation related attributes to validate data.

  1. Email validation
[EmailValidation(ErrorMessageResourceType = typeof(CommonLibrary.Resources), ErrorMessageResourceName = "ValEmailAddress")]
public string EmailAddress { get; set; }

Here CommonLibrary.Resources is class of resource file.

2.  Numeric Validation

[NumbericValidationAttribute(ErrorMessageResourceType = typeof(CommonLibrary_Resources), ErrorMessageResourceName = "ValBirthYear")]
public int? BirthYear { get; set; }

Here CommonLibrary.Resources is class of resource file.

3.  Numeric Validation with decimal support

[NumbericDecimalValidationAttribute(ErrorMessageResourceType = typeof(CommonLibrary_Resources), ErrorMessageResourceName = "VaSalary")]
public int? Salary { get; set; }

4. Date Range Validation

 

[FutueRangeValidationAttribute(ErrorMessageResourceType = typeof(VOT.Common.Resources), ErrorMessageResourceName = "ValJoinDate")]
public DateTime JoinDate { get; set; }

So in this why we can create any number of validation in MVC and easily attach this validation with model as an attributes.

Thanks,

Amit Patel

“Enjoy Programming”



Viewing all articles
Browse latest Browse all 11

Trending Articles