2
votes

I'm using jQuery unobtrusive validation version 2.0 for MVC3. I also have the latest jquery.validate (v 1.9). I have a popup form with this code:

$(document).ready(function () {
    $('#createForm').submit(function () {
        $.validator.unobtrusive.parse($('#createForm'));  
(more)

(The third line is necessary so that the form fields added by javascript will be validated.)

The validation works fine except that a value such as $1,000.00 entered into an input tag that is bound to a decimal property in the viewmodel is invalid, while 1,000.00 is valid. Clearly the "$" is casting the value as a string in the eyes of the validator.

I have researched this for many hours and I have only found one other similar question posted (also on SO and it was unanswered). I can't believe that this has been encountered by every MVC3 developer who handles currency values in a modal dialog, otherwise we would surely have some resolution by now, right?

I have resolved the issue on the server side by creating a DecimalBinder. Now I need a solution for the client-side validation. I have been looking hard at the API for jquery.validate.unobtrusive but I can't seem to find a hook. I do not want to modify any standard javascript library.

1
We use a money html extension method that creates a $ sign immediately preceding the input and inserts an attribute denoting its a monetary value. On change of elements with this attribute we strip the $ sign if its found in the inputs. I know this doesn't answer your question, but I believe not allowing dollar signs within inputs is the standard.DMulligan
I have the same problem. So far this bit of code is looking promising $.validator.methods.numberfrenchmd

1 Answers

0
votes

How about a custom validation method which first looks to strip off any $ characters prior to validating?

  var currentVal = (control's actual value);
  currentVal = currentVal.replace('$','');

Then continue with validation. The obvious downside is the need for custom validators.