title: MVC 3 - Restrict decimal data to 2 decimal places
body: <p>I have a project that I am developing using MVC 3 using Razor views.</p>
  
  <p>I have a Decimal data type where the user can enter an amount.  The data is submitted to a 3rd party where they insist the amount has a maximum of 2 decimal places.  The amount must be greater than zero.  The maximum amount varies depending on a value retrieved from the database.  So I can't hard code a maximum range of say 9999.99.  </p>
  
  <p>Example:- Let's say the maximum amount for the current transaction is 123.45.  I need to validate that the amount the user enters in the text box is between 0.01 and 123.45.  If the user enters 123.46 this would be invalid because it exceeds the maximum.  If the user enters 100.123 this would also be invalid because it would exceed the maximum of 2 decimal places.</p>
  
  <p>Research so far... I thought I would try and solve the decimal places issue first.  I was wondering if applying this to my model would work...</p>
  
  <pre><code>&lt;DisplayFormat(ApplyFormatInEditMode:=True, DataFormatString:="{0.00}")&gt;
  Public Property PaymentAmount As Decimal
  </code></pre>
  
  <p>View:-</p>
  
  <pre><code>@Html.TextBoxFor(Function(model) model.PaymentAmount)
  </code></pre>
  
  <p>... but this just seems to do nothing.</p>
  
  <p>Questions:-</p>
  
  <ol>
  <li><p>How can I create validation for a range restricted to 2 decimal places that accepts a value from 0.01 to X  (where X is a maximum pre-determined value returned from the database prior to displaying the view) ?</p></li>
  <li><p>Can this also work with unobtrusive client-side validation?</p></li>
  </ol>
  
  <p>I was thinking about creating a custom validation attribute and also hook this up for client-side validation which I think would work, but seems way like overkill for something that on the face of it would seem like a very simple issue.</p>