admin管理员组文章数量:1405578
I am using the following JavaScript function to add thousand separator for a input number on keyup event.
$("#inputId").on('keyup', function(evt){
if (evt.which != 110 && evt.which != 190){
var n = parseFloat($(this).val().replace(/\,/g,''),10);
$(this).val(n.toLocaleString());
}
});
Most of the cases this function is working as expected, but the only issue is the input field does not accept zero after the decimal point (for example 10.01) but it does accept numbers like 10.25 . I can't figure out the reason for this behavior. Any idea?
Thanks.
I am using the following JavaScript function to add thousand separator for a input number on keyup event.
$("#inputId").on('keyup', function(evt){
if (evt.which != 110 && evt.which != 190){
var n = parseFloat($(this).val().replace(/\,/g,''),10);
$(this).val(n.toLocaleString());
}
});
Most of the cases this function is working as expected, but the only issue is the input field does not accept zero after the decimal point (for example 10.01) but it does accept numbers like 10.25 . I can't figure out the reason for this behavior. Any idea?
Thanks.
Share Improve this question edited Feb 9, 2017 at 5:48 Ashish Bahl 1,5021 gold badge18 silver badges28 bronze badges asked Feb 9, 2017 at 4:40 LakmalLakmal 2571 gold badge10 silver badges25 bronze badges 9-
parsing would remove the
0
, since which doesn't have any value1.0
=>1
– Pranav C Balan Commented Feb 9, 2017 at 4:47 -
1
What does
parseFloat(x,10)
do? MDN says it has only one argument – Flying Gambit Commented Feb 9, 2017 at 4:48 -
parseFloat
doesn't have any radix argument.... so adding second argument doesn't make any sense – Pranav C Balan Commented Feb 9, 2017 at 4:49 - Out of curiosity, What exactly is a thousand separator ? – Flying Gambit Commented Feb 9, 2017 at 4:53
- 1 @FlyingGambit toLocaleString() will add the thousand separator(for me ","). – Lakmal Commented Feb 9, 2017 at 4:56
1 Answer
Reset to default 8Splitting on the decimal would work. Something like this. Note this is very basic starting point and does not account for the different separators in other countries/systems (ie: 10.000,00)
<script>
var myinput = document.getElementById('myinput');
myinput.addEventListener('keyup', function() {
var val = this.value;
val = val.replace(/[^0-9\.]/g,'');
if(val != "") {
valArr = val.split('.');
valArr[0] = (parseInt(valArr[0],10)).toLocaleString();
val = valArr.join('.');
}
this.value = val;
});
</script>
<input id="myinput" type="text'>
本文标签: jqueryAdd thousand separator with JavaScript on keyup eventStack Overflow
版权声明:本文标题:jquery - Add thousand separator with JavaScript on keyup event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744280343a2598619.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论