title: Create custom jquery validation rule based on custom localized error message asp core 3.1
body: <p><em><strong>Notes:</strong></em></p>
  <ol>
  <li>I am using Shared resource to localize error messages and it works for all .net annotation attributes</li>
  <li>I injected the dataannotation service to the startup class to accept the shared resource localization</li>
  </ol>
  <p>Here is my code for reviewing</p>
  <pre><code>public string PageName { get; set; }    //For custom validation purposes
  [Display(Name = &quot;Name&quot;)]
  [RequiredIf(&quot;PageName&quot;, &quot;Contacts&quot;, ErrorMessage = &quot;This field is required&quot;)]
  public string PageName { get; set; }
  
  
  public class RequiredIfAttribute : ValidationAttribute, IClientModelValidator
  {
      private String PropertyName { get; set; }
      private Object DesiredValue { get; set; }
      private readonly RequiredAttribute _innerAttribute;
      public RequiredIfAttribute(String propertyName, Object desiredvalue)
      {
          PropertyName = propertyName;
          DesiredValue = desiredvalue;
          _innerAttribute = new RequiredAttribute();
      }
  
      protected override ValidationResult IsValid(object value, ValidationContext context)
      {
  
          var dependentValue = context.ObjectInstance.GetType().GetProperty(PropertyName).GetValue(context.ObjectInstance, null);
  
          if (dependentValue != null &amp;&amp; dependentValue.ToString() == DesiredValue.ToString())
          //if (dependentValue == null || dependentValue.ToString() != DesiredValue.ToString())
          {
              if (!_innerAttribute.IsValid(value))
              {
                  // if validation result is wrong
                  return new ValidationResult(ErrorMessage);
              }
          }
          // if validation result is correct
          return ValidationResult.Success;
      }
  
  
      public void AddValidation(ClientModelValidationContext context)
      {
          if (context == null)
          {
              throw new ArgumentNullException(&quot;context&quot;);
          }
          context.Attributes.Add(&quot;data-val&quot;, &quot;true&quot;);
          context.Attributes.Add(&quot;data-val-requiredIf&quot;, ErrorMessage);  //Here i need to get the localized message
          context.Attributes.Add(&quot;data-seed-property&quot;, PropertyName);
          context.Attributes.Add(&quot;data-desired-value&quot;, DesiredValue.ToString());
      }
  }
  </code></pre>