title: ASP.NET MVC custom validator client-side validation not firing
body: <p>I have created a custom group validator that ensure that the user has filled in at least one field from a given group. This works when I load the page and submit the form without entering anything.</p>
  
  <h3>Invalid Form </h3>
  
  <p><a href="https://i.stack.imgur.com/UCTA3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UCTA3.png" alt="enter image description here"></a></p>
  
  <h3>Valid Form </h3>
  
  <p><a href="https://i.stack.imgur.com/ctOaB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ctOaB.png" alt="enter image description here"></a></p>
  
  <p>However, the issue is whenever the form has any error other than the group validation error. When I force the form to display the group required validation error and then display, for example, a string length error on one of the fields in the group the group validation error doesn't disappear.  </p>
  
  <p><a href="https://i.stack.imgur.com/KEO2x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KEO2x.png" alt="enter image description here"></a></p>
  
  <p>After a bit of debugging I'm fairly certain that its the client side validation that is not working correctly as its not removing the server side validation error whenever a client side validation is generated. When the form generates the string length error it prevents the form from being posted to the server which would remove the group validation required error.</p>
  
  <p>Can anyone see an issue with the client side validation code? I copied the code from this stackoverflow page <a href="https://stackoverflow.com/questions/5816313/mvc3-unobtrusive-validation-group-of-inputs">MVC3 unobtrusive validation group of inputs</a> and and changed it to meet my needs. This is the first time I have attempted custom validation so forgive me if the issue is something really simple. Another indicator is that whenever I enter and then remove a single character the group validation error doesn't turn on/off.</p>
  
  <p>I have provided the custom validator code below:</p>
  
  <h2> Custom Validator C# (Serverside and Clientside) </h2>
  
  <pre><code>public class RequireAtLeastOneIfAttribute : ValidationAttribute, IClientValidatable
  {
      private string[] Properties { get; set; }
      public RequireAtLeastOneIfAttribute(params string[] properties)
      {
          Properties = properties;
      }
  
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
        if(Properties == null || Properties.Length &lt; 1)
        {
           return null;
        }
  
        foreach (var property in Properties)
        {
           var propertyInfo = validationContext.ObjectType.GetProperty(property);
           var propertyValue = propertyInfo.GetValue(validationContext.ObjectInstance, null);
  
           if (propertyInfo == null)
           {
              return new ValidationResult(string.Format("Unknown property {0}", property));
           }
  
           if (propertyValue is string &amp; !string.IsNullOrEmpty(propertyValue as string))
           {
              return null;
           } 
  
           if (propertyValue != null)
           {
              return null;
           }
        }
  
        return new ValidationResult("At least one age group field is required");
     }
  
     public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
     {
        var rule = new ModelClientValidationRule
        {
           ErrorMessage = ErrorMessage,
           ValidationType = "requireatleaseoneif"
        };
  
        rule.ValidationParameters["properties"] = string.Join(",", Properties);
  
        yield return rule;
     }
  }
  </code></pre>
  
  <h2> Javascript </h2>
  
  <p>The only changes I made to the javascript is the id.</p>
  
  <pre><code>jQuery.validator.unobtrusive.adapters.add(
      'requireatleaseoneif', ['properties'], function (options) {
          options.rules['requireatleaseoneif'] = options.params;
          options.messages['requireatleaseoneif'] = options.message;
      }
  );
  
  jQuery.validator.addMethod('requireatleaseoneif', function (value, element, params) {
      var properties = params.properties.split(',');
      var values = $.map(properties, function (property, index) {
          var val = $('#' + property).val();
          return val != '' ? val : null;
      });
      return values.length &gt; 0;
  }, '');
  </code></pre>
  
  <h2> Razor </h2>
  
  <pre><code>@Html.RadioButtonFor(m =&gt; m.HaveDependants, true, new { id = "borrower-haveDependants-true", @class = "block-radio-input" })
  @Html.RadioButtonFor(m =&gt; m.HaveDependants, false, new { id = "borrower-haveDependants-false", @class = "block-radio-input" })             
  @Html.ValidationMessageFor(m =&gt; m.HaveDependants)
  
  @Html.LabelFor(m =&gt; m.DependantsAgeGroupOne)                            
  @Html.TextBoxFor(m =&gt; m.DependantsAgeGroupOne, new { @class = "form-control" })
  @Html.ValidationMessageFor(m =&gt; m.DependantsAgeGroupOne)
  
  @Html.LabelFor(m =&gt; m.DependantsAgeGroupTwo)
  @Html.TextBoxFor(m =&gt; m.DependantsAgeGroupTwo, new { @class = "form-control" })
  @Html.ValidationMessageFor(m =&gt; m.DependantsAgeGroupTwo)
  
  @Html.LabelFor(m =&gt; m.DependantsAgeGroupThree)
  @Html.TextBoxFor(m =&gt; m.DependantsAgeGroupThree, new { @class = "form-control" })
  @Html.ValidationMessageFor(m =&gt; m.DependantsAgeGroupThree)
  </code></pre>
  
  <p>I also that the issue might be with how its triggered. Because the validation attribute isn't on the text input themselves but rather on the radio button I added the following javascript to validate on the whole form on button click.</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function () {
          $(form).validate;
      });
  &lt;/script&gt;
  </code></pre>
  
  <h2>I changed the last bit of Javascript to the following </h2>
  
  <pre><code>$('input').blur(function () {
     $('.haveDependants').valid();
  })
  </code></pre>