Custom validation attribute in asp net core

preview_player
Показать описание

Text version of the video

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.

Slides

ASP.NET Core Text Articles & Slides

ASP.NET Core Tutorial

Angular, JavaScript, jQuery, Dot Net & SQL Playlists

ASP.NET Core built-in attributes

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

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)
{
}

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

Using the Custom Validation Attribute

public class RegisterViewModel
{
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 failes.
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.
Рекомендации по теме
Комментарии
Автор

a whole academy in one person, thank you so much CREATIVE GUY

MmMm-tgmq
Автор

You are a gifted teacher! I understand everything you say, you are very articulate on explaining them without making me feel like I'm stupid. I have this issue to create custom validation on one special field called cost centre based on SAP validation. I'm going to try this next Monday. Wished me luck!

rmm
Автор

You are the great lecturer I ever met. all the best

menakasattmann
Автор

Hi Venkat how to make a custom validation attribute to work on client side.

vssrikanthgarnepudi
Автор

Super venkat and one more thing i don't know why all people skip custom client side validation for Core version. Please consider

vinothdharmaraj
Автор

Sir, how can we have client side validations for custom attr

shahidwani
Автор

This video concludes saying it works "just like any other validation attribute" that is not true. There is no client side validation, so the form must post before the attribute runs. That is problematic if the user has filled out form fields that depend on dynamic controls because they will lost their data.

stdcarriers
Автор

I affraid that anyone type only "test@" and click register your code crash :) But it's a detail. Great video, like always!

maikeru
Автор

why do you use the ErrorMessage prop from the base class, isn't there a way to set it through the base() ctor?

mati
Автор

What about custom validations for client and remote?

seneysean
Автор

How can you trigger the custom validation attribute on lost focus? I want my custom validation to work exactly like the EmailAddress or Required attribute

ionutenache
Автор

If I have an integer type attribute but the value entered by the user is greater than the maximum value allowed for integers, how do I validate it and display a custom message?

LUISEGONZALEZL
Автор

Hey, thank you once again for your detailed explanation. I guess you cannot do something similar to this if you are using a shared class library, can you? My models are stored in the class library adjacent to my web project. I have a reference from my web project to the class library but it will not allow me to use the custom attribute from the class library back to the web project. It's working to an extent as long as the custom validator is coming from the class library but since the custom validator is not in the web project, it's not showing the validation in red when I hit submit. But it's partially working as long as I have a "." in my input field which is what I did for validating only a domain name like "my.domain". I have to enter a "." in the input field or else it fails, I just wish the red validation would cooperate :/ Thanks.

muttBunch
Автор

Thanks for great explanation, Please create a video on client side custom validation as well.

manishkm
Автор

Hi Venkat, will you be covering Fluid Validation in this series?

steveymcneckbeard
Автор

wouldnt that be much easier to just call

МаркВирченко-жщ
Автор

Sir, When i am defining [ValidEmailDomain] attribute in RegisterViewModel class and press [ctrl .] then it is not recognising EmployeeManagement.Utilities namespace ?

payalagarwal
Автор

how can you implement custom attribute on client side ????

loveunimeanit
Автор

Hi, Anyone can you help. For Some reason the IsValid Method doesnt fire for me. I have no Compile time errors. Any ideas?

ransack
Автор

Kudvenkat Sir, I have done exact replications of the steps that you have shown in video still my IsValid function is getting called only on click of Submit nor I have any compile time errors, what might be the reason? Can you please help?

kunalshah
join shbcf.ru