Custom validation attribute in asp net core

Поділитися
Вставка
  • Опубліковано 24 січ 2025

КОМЕНТАРІ • 63

  • @MmMm-tg5mq
    @MmMm-tg5mq 5 років тому +12

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

  • @rmm1332
    @rmm1332 2 роки тому +2

    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!

  • @menakasattmann5335
    @menakasattmann5335 5 років тому +2

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

  • @shahidwani6445
    @shahidwani6445 5 років тому +13

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

    • @Abhimanyukumar-vb8bz
      @Abhimanyukumar-vb8bz 4 роки тому

      Do you got the answer??

    • @gabrielbarreto5377
      @gabrielbarreto5377 4 роки тому +1

      This worked to me.
      //Validation attribute
      public class ValidEmailDomainAttribute :ValidationAttribute, IClientModelValidator
      {
      public string AllowedDomain { get; }
      public ValidEmailDomainAttribute(string AllowedDomain)
      {
      this.AllowedDomain = AllowedDomain;
      }
      public override bool IsValid(object value)
      {
      return (value as string).EndsWith(AllowedDomain);
      }
      public void AddValidation(ClientModelValidationContext context)
      {
      if (context == null)
      {
      throw new ArgumentNullException(nameof(context));
      }
      context.Attributes.Add("data-val", "true");
      context.Attributes.Add("data-val-ValidEmailDomain","Error: "+ ErrorMessage);
      context.Attributes.Add("data-val-AllowedDomain", AllowedDomain);
      }
      }
      //javascript in the view
      (function ($) {
      $.validator.addMethod('data-val-ValidEmailDomain',
      function (value, element, params) {
      debugger;
      value.slice()
      var domain = $(element).attr('data-val-AllowedDomain');
      if (domain) {
      return value.slice(-1 * domain.length) === domain;
      }
      return false;
      }, function (params, element) {
      debugger;
      var msgCompare = $(element).attr('data-val-ValidEmailDomain');
      if (!msgCompare) {
      msgCompare = 'Domain of email is not allowed.';
      }
      return msgCompare;
      });
      $.validator.unobtrusive.adapters.addBool('data-val-ValidEmailDomain');
      })(jQuery);
      There is another example in the below link[1], I used it as reference ( but the article is in portuguese).
      [1] medium.com/@fulviocanducci/aspnet-core-valida%C3%A7%C3%A3o-customizadas-server-side-e-client-side-55a7972757d4

    • @leozhang4574
      @leozhang4574 4 роки тому +2

      Good question. Maybe we have to write the validation logic as a "Remote" action instead of a custom validation attribute?

    • @debarghyachakraborty
      @debarghyachakraborty 4 роки тому

      @@gabrielbarreto5377 Thanks, worked.

  • @vssrikanthgarnepudi5952
    @vssrikanthgarnepudi5952 5 років тому +5

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

  • @maikeru_pk
    @maikeru_pk 5 років тому +2

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

    • @admiremhlaba1192
      @admiremhlaba1192 5 років тому +2

      Thank you for the pointer, I missed this one too, yes index 1 would be null and an exception would be raised....but I noticed that I have EmailAddress DataAttribute on Email Property so test@ would be an invalid email address that the client-side validation would pickup and block posting it.

    • @maikeru_pk
      @maikeru_pk 5 років тому

      @@admiremhlaba1192 Kud Venkat is a woman? 🤔

    • @admiremhlaba1192
      @admiremhlaba1192 5 років тому

      @@maikeru_pk I don't believe you but thanks for the heads up

    • @maikeru_pk
      @maikeru_pk 5 років тому

      @@admiremhlaba1192 No no it's just a joke :) Sorry, God bless you Admire :)

    • @admiremhlaba1192
      @admiremhlaba1192 5 років тому

      @@maikeru_pk Be Blessed More My Human Friend - I don't know your gender so I'm using Generics

  • @levitkanner6055
    @levitkanner6055 3 роки тому

    You're amazing!

  • @vinothdharmaraj7510
    @vinothdharmaraj7510 5 років тому

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

  • @mati2901
    @mati2901 10 місяців тому

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

  • @sakthir3892
    @sakthir3892 5 років тому

    Nice explanation sir

  • @muttBunch
    @muttBunch Рік тому

    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.

  • @stdcarriers693
    @stdcarriers693 4 роки тому +1

    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.

  • @payalagarwal8564
    @payalagarwal8564 4 роки тому

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

    • @misaghsh9517
      @misaghsh9517 3 роки тому

      for sure you have misspelling or typo error. Check and make sure you type class name correctly.

  • @LUISEGONZALEZL
    @LUISEGONZALEZL 4 роки тому

    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?

  • @ionutenache6473
    @ionutenache6473 3 роки тому

    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

  • @naodagere8210
    @naodagere8210 4 роки тому

    Thank you sir.

  • @seneysean
    @seneysean 5 років тому +1

    What about custom validations for client and remote?

  • @mohammadmkhani105
    @mohammadmkhani105 2 роки тому

    thank you

  • @manishk009m
    @manishk009m 5 років тому

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

    • @Csharp-video-tutorialsBlogspot
      @Csharp-video-tutorialsBlogspot  5 років тому

      Hello Manish - Client side validation is covered in details in Parts 74 to 76. Here is the link to complete ASP.NET Core MVC tutorial.
      www.pragimtech.com/courses/asp-net-core-mvc-tutorial-for-beginners/

    • @vinothdharmaraj7510
      @vinothdharmaraj7510 5 років тому

      @@Csharp-video-tutorialsBlogspot No venkat, this custom attribute only working on server side only and we should write for client side also.

    • @marconagel3018
      @marconagel3018 4 роки тому +1

      @@vinothdharmaraj7510 Taking into consideration that maybe you want 2 remote validations for the same field that is email, .net core doesnt let you add multiple remote attributes so, you could create a method IsEmailValid and check within if the email is in use or if the email match with domain "PREGIM.COM" like Lin mentioned here stackoverflow.com/questions/20329121/multiple-remote-validation-attribute-in-asp-net-mvc4.
      Hope it helps you Vinoth, Venkat I admire you everyday, thanks for giving us free education for future engineers or good programmers, regards..

  • @ransack9385
    @ransack9385 4 роки тому +1

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

    • @kunalshah7270
      @kunalshah7270 4 роки тому

      Sir even my IsValid overridden function is not getting executed, did you found the reason? Please share.

    • @Abhimanyukumar-vb8bz
      @Abhimanyukumar-vb8bz 4 роки тому

      @@kunalshah7270 what is happening can you elaborate

    • @rmm1332
      @rmm1332 2 роки тому

      Did you get any solution to this? I haven't try it yet but I have high hope for it to works 😅 seeing this message making the hope seems a bit dimmer

  • @robertmays6097
    @robertmays6097 5 років тому

    great

  • @kunalshah7270
    @kunalshah7270 4 роки тому

    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?

    • @lifetraveler8008
      @lifetraveler8008 4 роки тому

      Did you find the solution? I am also having the same issue

    • @kunalshah7270
      @kunalshah7270 4 роки тому

      @@lifetraveler8008 No sir not yet.

  • @МаркВирченко-ж6щ
    @МаркВирченко-ж6щ 5 років тому +1

    wouldnt that be much easier to just call value.EndsWith(allowedDomain)?

    • @МаркВирченко-ж6щ
      @МаркВирченко-ж6щ 5 років тому

      and safer

    • @roy_antony
      @roy_antony 4 роки тому +1

      Don't think so. ex: allowed domain is "mail.com".....I can add "gmail.com". It is better to split the string with '@'.

  • @loveunimeanit
    @loveunimeanit 5 років тому

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

  • @genovme
    @genovme 4 роки тому

    How can I allowed more than one domain?

    • @abderrahmanrahou9189
      @abderrahmanrahou9189 4 роки тому +2

      Hi, to allow more than one domain you should pass an Array of string, and this is code source:
      for RegisterViewModel class:
      [RegisterEmailDomainValidation(AllowedDomain: new string[] {"gmail.com","hotmail.com"}
      ,ErrorMessage ="This email domain should be: gmail.com or hotmail.com")]
      public string Email { get; set; }
      ---------------------------------------------------------------------------------------------------------------------------------------------------------
      For ValideEmailAttribute class:
      public class ValideEmailAttribute : ValidationAttribute
      {
      private readonly string[] AllowedDomain;
      public ValideEmailAttribute (string[] AllowedDomain)
      {
      this.AllowedDomain = AllowedDomain;
      }
      public override bool IsValid(object value)
      {

      string[] splitedEmail = value.ToString().Split("@");
      foreach(var v in AllowedDomain)
      {
      if(v.ToUpper()==splitedEmail[1].ToUpper())
      {
      return true;
      }

      }
      return false;
      }
      }
      Hope this code is helpful, have a nice chance.

    • @javaguitarist
      @javaguitarist 4 роки тому

      @@abderrahmanrahou9189 Thank you kindly for this idea. I have a tested the following solution.
      VALIDEMAILDOMAINATTRIBUTE
      public class ValidEmailDomainAttribute : ValidationAttribute
      {
      private readonly string[] allowedDomain;
      public ValidEmailDomainAttribute(string[] allowedDomain)
      {
      this.allowedDomain = allowedDomain;
      }
      public override bool IsValid(object value)
      {
      foreach (var dom in allowedDomain) {
      string[] emailDomain = value.ToString().Split("@");
      string thisEmailDomain = emailDomain[1];
      string domElement = dom.ToString().ToUpper();
      if (thisEmailDomain.ToString().ToUpper() == domElement.ToUpper())
      {
      return thisEmailDomain == dom;
      }
      }
      return false;
      }
      }
      REGISTERVIEWMODEL
      public class RegisterViewModel
      {
      public string City { get; set; }
      [Required]
      [EmailAddress]
      [Remote(action: "IsEmailInUse", controller: "Account")]
      [ValidEmailDomain(allowedDomain: new string[] { "abc.com", "xyz.com" },
      ErrorMessage = "Email domain must be abc.com or xyz.com.")]
      public string Email { get; set; }
      [Required]
      [DataType(DataType.Password)]
      public string Password { get; set; }
      [DataType(DataType.Password)]
      [Display(Name ="Confirm Password")]
      [Compare("Password",
      ErrorMessage ="Password and confirmation do not match.")]
      public string ConfirmPassword { get; set; }
      }
      }

  • @steveymcneckbeard
    @steveymcneckbeard 5 років тому

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

  • @weichengchen4598
    @weichengchen4598 5 років тому

    This is how you do client side validations for custom attribute.
    stackoverflow.com/questions/41900485/custom-validation-attributes-comparing-two-properties-in-the-same-model

  • @hossammetwally07
    @hossammetwally07 5 років тому

    Why should we use ToUpper() function?

    • @IamSandeepKmr
      @IamSandeepKmr 5 років тому +2

      What if the domain name you want to validate with is pragimtech.com and user enters Pragimtech.com. The validation will fail but it is not advisable to make it case sensitive comparison thats the reason.

    • @hossammetwally07
      @hossammetwally07 5 років тому

      @@IamSandeepKmr Thanks bro

  • @jeffreyhermosa
    @jeffreyhermosa 5 років тому

    Video thumbnail is wrong, but as always great video

    • @javaguitarist
      @javaguitarist 4 роки тому

      Once I found myself unwittingly 10 videos ahead of the next one, so I think this happened before also.

  • @Suecia2020
    @Suecia2020 4 роки тому

    Thank you sir.