title: Custom Validation for Checkbox in MVC5 razor
body: <p>I am trying to force a required terms and conditions checkbox for a view.  I followed the code sample outlined on this forum: <a href="http://www.dotnet-tricks.com/Tutorial/mvc/8UFa191212-Custom-Validation-for-Checkbox-in-MVC-Razor.html" rel="nofollow">http://www.dotnet-tricks.com/Tutorial/mvc/8UFa191212-Custom-Validation-for-Checkbox-in-MVC-Razor.html</a> but if I set a breakpoint within the IsValid method, is it never reached.  Also, the ModelState.IsValid returns true when it should be false; if the checkbox is never selected. </p>
  
  <p>Here is the code for the ViewModel:</p>
  
  <pre><code>public class TermsConditionViewModel
  {
      [MustBeTrue(ErrorMessage = "The terms and conditions must be read and agreed to complete the registration process.")]
      [Display(Name = "")]
      public bool TermsConditionsCompleted { get; set; }
  } 
  </code></pre>
  
  <p>Here is the code for the custom data annotations:</p>
  
  <pre><code>public class MustBeTrueAttribute : ValidationAttribute, IClientValidatable // IClientValidatable for client side Validation
  {
      public override bool IsValid(object value)
      {
          return value is bool &amp;&amp; (bool)value;
      }
      // Implement IClientValidatable for client side Validation
      public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
      {
          return new ModelClientValidationRule[] { new ModelClientValidationRule { ValidationType = "checkbox", ErrorMessage = this.ErrorMessage } };
      }
  
  
  }
  
  public class MustBeSelected : ValidationAttribute, IClientValidatable // IClientValidatable for client side Validation
  {
      public override bool IsValid(object value)
      {
          if (value == null || (int)value == 0)
              return false;
          else
              return true;
      }
      // Implement IClientValidatable for client side Validation
      public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
      {
          return new ModelClientValidationRule[] { new ModelClientValidationRule { ValidationType = "dropdown", ErrorMessage = this.ErrorMessage } };
      }
  }
  </code></pre>
  
  <p>Here is the javascript code for the client side validation</p>
  
  <pre><code>$.validator.unobtrusive.adapters.add("dropdown", function (options) {
  //  debugger;
  if (options.element.tagName.toUpperCase() == "SELECT" &amp;&amp; options.element.type.toUpperCase() == "SELECT-ONE") {
      options.rules["required"] = true;
      if (options.message) {
          options.messages["required"] = options.message;
      }
  }
  });
  
  $.validator.unobtrusive.adapters.add("checkbox", function (options) {
  if (options.element.tagName.toUpperCase() == "INPUT" &amp;&amp; options.element.type.toUpperCase() == "CHECKBOX") {
      options.rules["required"] = true;
      if (options.message) {
          options.messages["required"] = options.message;
      }
  }
  });
  </code></pre>
  
  <p>Here is the markup that is generated</p>
  
  <pre><code>&lt;input data-val="true" data-val-checkbox="The terms and conditions must be read and agreed to complete the registration process." data-val-required="The  field is required." id="TermsConditionsCompleted" name="TermsConditionsCompleted" type="checkbox" value="true" /&gt;&lt;input name="TermsConditionsCompleted" type="hidden" value="false" /&gt;&amp;nbsp;I have read and agree with the terms and conditions described above
  </code></pre>
  
  <p>Are there any extra configurations that need to take place for this to work?  All of the examples I found so far seem to be setup the same way, no configurations in Web.Config, Global.asax, etc.</p>