简体   繁体   English

MVC Jquery全球化 - 验证小数

[英]MVC Jquery globalization - validating a decimal

I am struggling to validate a decimal field using globalization. 我正在努力使用全球化来验证十进制字段。

I need my value to be in french format (with coma as decimal separator and no thousand separator). 我需要我的值是法语格式(昏迷为小数分隔符,没有千位分隔符)。

EDIT : updated following This great solution 编辑 :更新以下这个伟大的解决方案

So, here is my model : 所以,这是我的模型:

public class CompanyTaxHistoryModel
{
    [Required]
    public DateTime DateFrom { get; set; }

    [Required]
    public DateTime DateTo { get; set; }

    [Required]
    [Range(0, 100, ErrorMessage = "The percentage must be between 0 and 100")]
    [DisplayFormat(DataFormatString = "{0:N} %")]
    [Display(Name = "Company Tax")]
    public decimal CompanyTaxPercent { get; set; }

}

here is my web.config : 这是我的web.config:

<globalization requestEncoding="UTF-8" responseEncoding="UTF-8" culture="auto" uiCulture="auto" / > <globalization requestEncoding="UTF-8" responseEncoding="UTF-8" culture="auto" uiCulture="auto" / >

I have added a "localizationHelper" as follows : 我添加了一个“localizationHelper”如下:

namespace xxxx.Helpers
{
    public static class LocalizationHelpers
    {
        public static IHtmlString MetaAcceptLanguage<t>(this HtmlHelper<t> html)
        {
            var acceptLanguage = HttpUtility.HtmlAttributeEncode(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString());
            return new HtmlString(String.Format("<meta name=\"accept-language\" content=\" {0}\">",acceptLanguage));

        }
    }
}

Wich i use in my _Layout.cshtml : 我在_Layout.cshtml中使用的:

@using xxxx.Helpers
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    @Html.MetaAcceptLanguage() 

I have added the Globalize.js + the needed cultures to my project and added the below to my scripts : 我已将Globalize.js +所需的文化添加到我的项目中,并将以下内容添加到我的脚本中:

$(document).ready(function () {
    var data = $("meta[name='accept-language']").attr("content")
    Globalize.culture(data);
});

$.validator.methods.date = function (value, element) {
    return Globalize.parseDate(value);
};

$.validator.methods.range = function (value, element, param) {
    return this.optional(element) || (Globalize.parseFloat(value) >= param[0] && Globalize.parseFloat(value) <= param[1]);
}

$.validator.methods.number = function (value, element) {
    return !isNaN(Globalize.parseFloat(value));
}

Everything seems to be working just fine but I have a problem with the decimal values as the "." 一切似乎工作得很好,但我有一个问题,十进制值为“。” button of any keyboard should render the appropriated value (either a "." or a "," according to the local culture). 任何键盘的按钮都应该呈现适当的值(根据当地文化,“。”或“,”)。

For example, my browser is set to fr-FR and if I type "12,5" using the numeric pad only, I get "12.5" and therefore I get a validation error. 例如,我的浏览器设置为fr-FR,如果我只使用数字键盘输入“12,5”,我得到“12.5”,因此我得到验证错误。

In such case the "." 在这种情况下,“。” button of the numeric pad shall be understood as a coma. 数字键盘的按钮应理解为昏迷。

any ideas, what am I missing ? 任何想法,我错过了什么?

So, 所以,

for the globalization issue, the solution was here and reflected in the edit of my question (so far seems to work perfectly). 对于全球化问题,解决方案在这里并反映在我的问题的编辑中(到目前为止似乎工作得很好)。

as for the point/comma issue here is what i did 至于点/逗号问题,这就是我所做的

function validateFrNumber(e) {

        theCulture = Globalize.cultureSelector;
        foundFR = theCulture.indexOf("FR")
        foundfr = theCulture.indexOf("fr")

        if (foundFR != '-1' || foundfr != '-1') {
            theValeur = $(e).find("input").val();
            foundDot = theValeur.indexOf(".");

            if (foundDot > -1) {
                $(e).find("input").attr('value', theValeur.replace(".", ","));
            }
        }

}

and then call this in the input onkeyup: 然后在输入onkeyup中调用它:

<div class="input" onkeyup="validateFrNumber(this)">@Html.EditorFor(model => model.MydecimalValue)</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM