Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Custom validation attribute in asp.net core

Suggested Videos
Part 73 - Open redirect vulnerability example | Text | Slides
Part 74 - ASP.NET Core client side validation | Text | Slides
Part 75 - ASP.NET core remote validation | Text | Slides

In this video we will discuss how to create a custom validation attribute in asp.net core.

ASP.NET Core built-in attributes

For most use cases asp.net core has several built-in attributes for model validation. We discussed some of these attributes in Parts 42 and 43 of ASP.NET Core tutorial. Some of the built-in attributes are listed below.

  • Required
  • Range
  • StringLength
  • Compare
  • Regular Expression

Custom Attribute in ASP.NET Core

If you have a complex validation requirement that you cannot implement using the built-in attributes, you can create a custom validation attribute and reuse it in your project or even in multiple projects if you create it in a separate class library project.

Custom Validation Attribute Example

Consider the following User Registration Page. Our business requirement is to only allow email address where the domain name is pragimtech.com. If any other domain name is used, we want to display a validation error. We could achieve this using the built-in regular expression validator, but let's create a custom validator.

custom validation attribute in asp.net core

ValidationAttribute class in ASP.NET Core

To create a custom validation attribute, create a class that derives from the built-in abstract ValidationAttribute class and override IsValid() method.

public class ValidEmailDomainAttribute : ValidationAttribute
{
    private readonly string allowedDomain;

    public ValidEmailDomainAttribute(string allowedDomain)
    {
        this.allowedDomain = allowedDomain;
    }

    public override bool IsValid(object value)
    {
        string[] strings = value.ToString().Split('@');
        return strings[1].ToUpper() == allowedDomain.ToUpper();
    }
}

Using Custom Validation Attribute in ASP.NET Core

public class RegisterViewModel
{
    [ValidEmailDomain(allowedDomain: "pragimtech.com",
        ErrorMessage = "Email domain must be pragimtech.com")]
    public string Email { get; set; }

    // Other Properties
}
  • Use the custom validation attribute just like any other built-in validation attribute.
  • Email property is decorated with ValidEmailDomain attribute which is our custom validation attribute.
  • AllowedDomain property specifies the email domain that we want to validate against.
  • ErrorMessage property specifies the error message that should be displayed if the validation fails.
  • The ErrorMessage property is inherited from the built-in base class ValidationAttribute.
  • The validation error message is then picked up and displayed by the built-in validation tag helper.
asp.net core tutorial for beginners

2 comments:

  1. Hi sir, is there any way to use this custom validation attribute as a remote validation attribute ??
    I am looking for you answer.

    ReplyDelete
  2. Is it possible to call ValidEmailDomainAttribute method remotely?

    ReplyDelete

It would be great if you can help share these free resources