使用.Net MVC Project上的本地号码全球化错误
我试图validation本地十进制数字(葡萄牙语 – > pt-PT),但我在浏览器中收到控制台错误:
Uncaught TypeError: t.parseFloat is not a function. Exception occurred when checking element SizeOpenedWidth, check the 'number' method.
在葡萄牙,小数点分隔符是“,”,因此这里的有效十进制数是“10,21”。
我用来加载全球化的脚本:
$.when( $.getJSON("/Scripts/cldr/supplemental/likelySubtags.json"), $.getJSON("/Scripts/cldr/main/numbers.json"), $.getJSON("/Scripts/cldr/main/ca-gregorian.json"), $.getJSON("/Scripts/cldr/supplemental/timeData.json") ).then(function () { // Normalize $.get results, we only need the JSON, not the request statuses. return [].slice.apply( arguments, [ 0 ] ).map(function( result ) { return result[ 0 ]; }); }).then( Globalize.load ).then(function() { Globalize.locale("pt-PT"); });
在视图上我使用不显眼的validation:
@Html.LabelFor(model => model.SizeOpenedWidth, htmlAttributes: new { @class = "control-label col-md-3" }) @Html.EditorFor(model => model.SizeOpenedWidth, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.SizeOpenedWidth, "", new { @class = "text-danger" })
要加载所有库,我使用共享/布局文件:
该问题与jQuery Validation Globalize Library版本有关。
我使用的是仍在使用parseFloat方法的1.0.1版本。
使用Globalize v1.0.0时,此方法不再可用,它已被Globalize.parseNumber方法替换。
更新到1.1.0版后,一切都开始正常工作。