如何在jquery中允许逗号作为小数分隔符? [英] How do I allow comma as a decimal separator in jquery?

查看:100
本文介绍了如何在jquery中允许逗号作为小数分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个文本输入,必须在.blur()上修改德语价格格式的值。当用户按下小数分隔符的句点时它可以工作,但由于我有一个德语键盘,我想按逗号来获得相同的结果。我在下面添加了一个代码片段进行演示。如果输入1545454.154,输入结果将为1.545.454,15(由于toLocalString DE)。如果我用行中的逗号更改句点(/[^0-9 \。] / g,''); to(/ [^ 0-9 \,] / g,'')我现在可以按逗号,但结果将是1.545.454,00。所以代码没有看到我的逗号作为分隔符。我知道我应该使用.replace(,,。),但我不知道放在哪里。提前感谢您的帮助。



 $(窗口).ready(function(){
function validateNumber(event){
this.value = this.value.replace(/ [^ 0-9 \。] / g,'');
};
$('#int')。keyup (validateNumber);
$('#float1')。keyup(validateNumber);
$('#float1')。on(blur,formatDecimal);

function formatDecimal(event){
console.log($(this).attr('decimalplaces'));
var number = parseFloat(this.value);
this.value = isNaN (数字)?'0':number.toLocaleString('de-DE',{
maximumFractionDigits:2,
style:'currency',
currency:'EUR'
};
};

$('#float2')。keyup(validateNumber);
$('#float2')。on(blur,formatDecimal) ;
var dt = dateFormatter('#date');
$('[id ^ = date]')。keypress(validateNumber);
});

解决方案

(窗口).ready(function(){
function validateNumber(event){
this.value = this.value.replace(/ [^ 0-9 \。] / g,'');
};


('#int ').keyup(validateNumber);

(' #float1' )KEYUP(validateNumber);

I have this text input that has to convent the value in the German price format on .blur(). It works when the user presses period for the decimal separator, but since I have a German keyboard I would like to press the comma to get the same results. I added a code snippet below for a demonstration. With this if I type 1545454.154 the input will result in 1.545.454,15 (thanks to the toLocalString DE). If I change the period with a comma in the line (/[^0-9\.]/g, ''); to (/[^0-9\,]/g, '') I can now press my comma but the result will be 1.545.454,00. So the code is not seeing my comma as a separator. I know I should use .replace(",",".") but I do not know where to put it. Thanks in advance for any help.

$(window).ready(function() {
   function validateNumber(event) {
     this.value = this.value.replace(/[^0-9\.]/g, '');
   };
   $('#int').keyup(validateNumber);
   $('#float1').keyup(validateNumber);
   $('#float1').on("blur", formatDecimal);

   function formatDecimal(event) {
     console.log($(this).attr('decimalplaces'));
     var number = parseFloat(this.value);
     this.value = isNaN(number) ? '0' : number.toLocaleString('de-DE', {
       maximumFractionDigits: 2,
       style: 'currency',
       currency: 'EUR'
     });
   };

   $('#float2').keyup(validateNumber);
   $('#float2').on("blur", formatDecimal);
   var dt = dateFormatter('#date');
   $('[id^=date]').keypress(validateNumber);
 });

解决方案

(window).ready(function() { function validateNumber(event) { this.value = this.value.replace(/[^0-9\.]/g, ''); };


('#int').keyup(validateNumber);


('#float1').keyup(validateNumber);


这篇关于如何在jquery中允许逗号作为小数分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆