Globalize error with local numbers on .Net MVC Project
我正在尝试验证本地十进制数字(葡萄牙语 -> pt-PT),但在浏览器中出现控制台错误:
1 | Uncaught TypeError: t.parseFloat is not a function. Exception occurred when checking element SizeOpenedWidth, check the 'number' method. |
在葡萄牙,小数点分隔符是",",因此此处有效的十进制数是"10,21"。
我用来加载全球化的脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $.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"); }); |
在视图上我使用不显眼的验证:
1 2 3 4 5 | @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" }) |
要加载所有库,我使用共享/布局文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <script src="/Scripts/jquery/jquery-2.1.4.js"> <script src="/Scripts/cldr.js"> <script src="/Scripts/cldr/event.js"> <script src="/Scripts/cldr/supplemental.js"> <script src="/Scripts/cldr/unresolved.js"> <script src="/Scripts/globalize.js"> <script src="/Scripts/globalize/number.js"> <script src="/Scripts/globalize/date.js"> <script src="/Scripts/moment.min.js"> <script src="/Scripts/moment-with-locales.min.js"> <script src="/Scripts/bootstrap/bootstrap.js"> <script src="/Scripts/respond/respond.js"> <script src="/Scripts/jquery/jquery.validate.min.js"> <script src="/Scripts/jquery/jquery.validate.globalize.min.js"> <script src="/Scripts/jquery/jquery.validate.unobtrusive.min.js"> <script src="/Scripts/bootstrap-datetimepicker.js"> |
问题与 jQuery Validation Globalize Library 版本有关。
我使用的是 1.0.1 版本,它仍在使用 parseFloat 方法。
此方法在 Globalize v1.0.0 中不再可用,它已被替换为方法 Globalize.parseNumber.
更新到 1.1.0 版本后,一切都开始正常工作了。