views:

541

answers:

2

The new Asp.net mvc 2.0 input validation works like a charm, except for decimal seperators when it comes to jquery.validate. When I use the Microsoft MVC ajax libs it works fine. The comma is the decimal seperator server side as well as client side.

<script src="/Scripts/MicrosoftAjax.js" type="text/javascript" ></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript" ></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>

When I use this (jquery is loaded in the master view), I have to use a dot on the client side. But then the model is invalid and it gets returned back to the view, creating an interesting loop.

<script src="/Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcJqueryValidation.js" type="text/javascript"></script>

I'd rather use jquery for all my javascript needs, is there any way to make jquery.validate use the comma, and not the dot ? I have been googling and reading the docs, but weird enough I couldn't find much on this issue, which you would think would be a common one.

+2  A: 

Ahhh... The joys of globalization!

When dealing with number (and I won't even get started on dates) the need to determine the right decimal separator can be daunting. However, there's an easy way to determine the decimal separator for any client:

function GetDecimalSeparator()
{
  return  (1 / 10).ToString().substring(1, 1);
}

This way you'll always know what's the decimal separator at the client, and have the validation performed accordingly.

Paulo Santos
Ok, thanks. But how do I get jquery.validate to use this seperator ? And what if it's a dot and the MVC server side expects a comma ? This is getting very confusing ;).
Morph
A: 

Probably you will find the answer in the additional-methods.js. You will find the example of date localization: search for <input name="pippo" class="{dateITA:true}" /> and jQuery.validator.addMethod("dateITA", which follows. You can do the same to add and use your custom validation of numbers (corresponds to the locale which you use).

Oleg