How to create a custom validation attribute in asp.net core.
Text version of the video
https://csharp-video-tutorials.blogsp...
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.
/ @aarvikitchen5572
Slides
https://csharp-video-tutorials.blogsp...
ASP.NET Core Text Articles & Slides
https://csharp-video-tutorials.blogsp...
ASP.NET Core Tutorial
• ASP.NET core tutorial for beginners
Angular, JavaScript, jQuery, Dot Net & SQL Playlists
https://www.youtube.com/user/kudvenka...
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
On a new user registration page, we want 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.
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 the Custom Validation Attribute
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 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.
Watch video Custom validation attribute in asp net core online without registration, duration hours minute second in high quality. This video was added by user kudvenkat 25 June 2019, don't forget to share it with your friends and acquaintances, it has been viewed on our site 66,599 once and liked it 696 people.