0

I've a small problem with my Asp.Net MVC.

For now I've developed a website in english, everything is working fine, all my texts are already in localization, and I've already a session based system. When I go to another language(in my case french), it changes the Thread.CurrentThread.CurrentUICulture and Thread.CurrentThread.CurrentCulture to a culture info created from "fr".

All my traductions is working, but I've a problem in one form.

In France, the decimal part is separated from the integer part with ",". The server validation works fine with it and translate "234,512"(and know transforms "234.512" too)into a double.

But the client validation doesn't accept this, it says:

"Le champ XYZ doit être un nombre."

So it has the correct local(because the message is translated in french), but it seems to want the number edited in "235,55".

At start I thought it wasn't a big deal, users can enter number with the "234.512" format, but the problem is for the edition of data, since the thread is in French, it fill the model with "234,512", and not "234.512", and because of that customer must change the separator on every change(event if they don't make change on this field) otherwise they get a client validation error(no call are being made when I press on the submit button, I checked several times).

So what should I do to have the client validation script for double working in the current locale?

Thank you very much!

J4N
  • 19,480
  • 39
  • 187
  • 340

2 Answers2

2

It seems to be a problem with the jquery.validate.js (or jquery.validate.unobtrusive.js), which only accepts dots as separators.

Take a look on the following post for a solution: http://www.mfranc.com/2011/07/05/jquery-valdiator-modyfing-decimal-separator/

or

http://www.campusmvp.net/asp-net-mvc-3-and-the-coma-in-decimals/

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
1

It is known problem. Javascript validators inside jquery doesn't allow anything except point. The simplest way is to change jquery.validate.min.js (or so) or override custom validator inside your cshtml. Inside jquery it will be:

// http://docs.jquery.com/Plugins/Validation/Methods/number
number: function(value, element) {
    //return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value); <-- original 
    return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:[\.,]\d+)?$/.test(value); 
},