MVC3 edit for decimal fields and localization

前端 未结 2 766
闹比i
闹比i 2020-12-31 12:54

My locale uses a comma ,, and not a dot . for decimal separator.

In MVC3, when I open an Edit view where the decimal values are shown with

2条回答
  •  感情败类
    2020-12-31 13:04

    This blog post recommends overriding the default jQuery validate number and range rules in order to enable client-side support of the comma decimal separator.

    To fix these problems, we can take the default implementation from the jquery.validate.js file for the range() and number() functions. We then create another .js file (say jQueryFixes.js), in which we override these default functions with ones that contain support for the comma as a decimal separator. The contents of the file should be something like this:

    $.validator.methods.range = function (value, element, param) {
        var globalizedValue = value.replace(",", ".");
        return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
    }
    
    $.validator.methods.number = function (value, element) {
        return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:[\s\.,]\d{3})+)(?:[\.,]\d+)?$/.test(value);
    }
    

提交回复
热议问题