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

Custom validation attribute in 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 core.

ASP.NET Core built-in attributes

For most use cases 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 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 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: "",
        ErrorMessage = "Email domain must be")]
    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. core tutorial for beginners


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

  2. Is it possible to call ValidEmailDomainAttribute method remotely?


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