Mvc3 jquery unobtrusive validal locale decimal field

I am working on a localized mvc3 web application using unobtrusive validation. In web.config, I have:

<globalization culture="pl-PL" uiCulture="pl" /> 

Uses jQuery 1.4.4 and jquery 1.6 validation.

The problem is the decimal separator character.

I see that the jquery validation ignores the culture and expects the dot character to always be a decimal separator. Instead, I need to use a comma. I think this is the same in German.

I created my own methods_pl.js file:

 jQuery.extend(jQuery.validator.methods, { number: function(value, element) { return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value); } }); 

The main problem is solved above when the decimal number is not recognized at all.

But when I try to use RangeAttribute on my Decimal Price model, it still doesn't work. How to solve this?

+6
source share
2 answers

If you installed

 key="UnobtrusiveJavaScriptEnabled" value="false" 

DecimalModelBinder published in the previous answer will work, but you will disable clien's checks.

To solve your problem you need to enable the correct jQuery language. Check out this post from Scott Hanselmann or this post they should give you some help in its implementation.

You should add the form of the NuGet Jquery.globalize plugin and then add something like this in your DOMready function to have the correct number (at least it works for me)

  $.validator.methods.number = function (value, element) { if (Globalize.parseFloat(value)) return true; return false; } Globalize.culture('fr-FR'); 

then add the relative scripts on the page

 <script src="@Url.Content("~/Scripts/jquery-1.7.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.globalize/globalize.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.globalize/cultures/globalize.culture.fr-FR.js")" type="text/javascript"></script> 
+4
source

Phil Haack posted some information about this issue.

See here.

In a post, he describes the following:

 using System; using System.Globalization; using System.Web.Mvc; public class DecimalModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ValueProviderResult valueResult = bindingContext.ValueProvider .GetValue(bindingContext.ModelName); ModelState modelState = new ModelState { Value = valueResult }; object actualValue = null; try { actualValue = Convert.ToDecimal(valueResult.AttemptedValue, CultureInfo.CurrentCulture); } catch (FormatException e) { modelState.Errors.Add(e); } bindingContext.ModelState.Add(bindingContext.ModelName, modelState); return actualValue; } } 

and then at Global.asax

 ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder()); 

I'm not sure if this also fixes the problem on the client (Phil seems to indicate that it will work), but he should solve the problem on the server side, at least.

+1
source

Source: https://habr.com/ru/post/888730/


All Articles