title: How do I validate a HiddenField using MVC3 Razor & Unobtrusive jQuery validation?
body: <p>I have a Model similar to the following</p>
  
  <pre><code>public class TaskModel
  {
      public int UserId { get; set; }
      public int CustomerId { get; set; }
      public string CustomerDescription { get; set; }
      /* Snip */
  }
  </code></pre>
  
  <p>How can I add validation to ensure that, on client-side, a hidden field representing the <code>CustomerId</code> is greater than zero?</p>
  
  <pre><code>@Html.LabelFor(x =&gt; x.CustomerId)
  @Html.HiddenFor(x =&gt; x.CustomerId)
  &lt;label id="customerName" class="description"&gt;
      @Model.CustomerName
  &lt;/label&gt;
  </code></pre>
  
  <p>Just to explain what's going on above; I use a popup to select a customer from a list, which then populates the hidden field with the ID of the customer and updates the label description to the customer that the ID represents. I do it that way to avoiding an unnecessary post &amp; database call. </p>
  
  <p>I don't want to have to post the form data to determine if the ID is greater than 0. Is there an easy way to add this as both an Attribute to the Model and have it handled client side? I've written the following Validation attribute, but I don't know how to make it work seemlessly on the client-end as well.</p>
  
  <pre><code>public class ValidIDAttribute : ValidationAttribute
  {
      public override bool IsValid(object value)
      {
          if (value == null)
          {
              return false;
          }
          else
          {
              if (value.GetType() == typeof(int))
              {
                  return (int)value &gt; 0;
              }
              else if (value.GetType() == typeof(Guid))
              {
                  return (Guid)value != Guid.Empty;
              }
  
              return false;
          }
      }
  }
  </code></pre>