title: ASP.NET MVC3 Model Validation DataAnnotations to do less than or equal to another property with client side validation
body: <p>I have a simple form which is using ASP.NET MVC 3 unobtrusive client side validation.</p>
  
  <p>The model looks a bit like this (names changed for privacy):</p>
  
  <pre><code>public class MyInputModel
  {
      public MyInputModel()
      {
      }
  
      public MyInputModel(MyViewData viewData)
      {
          ViewData = viewData;
          MaxValueForSize = viewData.MaxSize;
      }
  
      public int MaxValueForSize { get; set; }
  
  
      [RegularExpression("[1-9][0-9]*",ErrorMessage = "The value must be a whole number.")]
      public int Size { get; set; }
  
      [StringLength(255)]
      [Required]
      public string Description{ get; set; }
  }
  </code></pre>
  
  <p>In my view I put a hidden field in for <code>MaxValueForSize</code> and I want to compare the entered value for <code>Size</code> to less than or equal to the <code>MaxValueForSize</code> property.</p>
  
  <p>I know I can do this server side by overriding validation attribute like so:</p>
  
  <pre><code>internal class SizeValidAttribute : ValidationAttribute
  {
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
          if(value != null)
          {
              var model = (MyInputModel) validationContext.ObjectInstance;
  
  
              if ((int)value &gt; model.MaxValueForSize)
                  return new ValidationResult(ErrorMessage);
  
          }
          return base.IsValid(value, validationContext);
      }
  }
  </code></pre>
  
  <p>However I would like to (need to) have client side validation on this property. Similar to how the <code>Compare</code> annotation works.</p>
  
  <p>Does anyone know of a way to do this? Do I need to extend the client side validation somehow?</p>
  
  <p>Thanks for your help.</p>