Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validate decimal separator and Asp.net MVC

The new Asp.net mvc 2.0 input validation works like a charm, except for decimal separators when it comes to jquery.validate. When I use the Microsoft MVC ajax libs it works fine. The comma is the decimal separator 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.

like image 873
Morph Avatar asked Jan 18 '10 12:01

Morph


2 Answers

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.

like image 81
Paulo Santos Avatar answered Nov 10 '22 05:11

Paulo Santos


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).

like image 38
Oleg Avatar answered Nov 10 '22 05:11

Oleg