I have a project that I am developing using MVC 3 using Razor views.
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.
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.
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…
<DisplayFormat(ApplyFormatInEditMode:=True, DataFormatString:="{0.00}")>
Public Property PaymentAmount As Decimal
View:-
@Html.TextBoxFor(Function(model) model.PaymentAmount)
… but this just seems to do nothing.
Questions:-
-
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) ?
-
Can this also work with unobtrusive client-side validation?
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.
I think you need to get maximum amount of a transaction from database. then you need to write like,
if the max amount of a transaction is fixed then there might be no issues you can use directly in javascript code. Hope it helps.