admin管理员组文章数量:1336617
How would I format amount field with ma and decimal?
I am able to get mas but this function does not allow decimal in the field.
$('input.number').keyup(function(event) {
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
;
});
});
How would I format amount field with ma and decimal?
I am able to get mas but this function does not allow decimal in the field.
$('input.number').keyup(function(event) {
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
;
});
});
Share
Improve this question
edited Sep 6, 2013 at 12:21
NDM
6,8403 gold badges41 silver badges54 bronze badges
asked Sep 6, 2013 at 12:05
Himanshu YadavHimanshu Yadav
13.6k49 gold badges170 silver badges295 bronze badges
1
- 2 Possible duplicate "How can I format numbers as money in JavaScript?"? – insertusernamehere Commented Sep 6, 2013 at 12:10
1 Answer
Reset to default 4Is this what you had in mind?
$('input.number').keyup(function(event) {
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/([0-9])([0-9]{2})$/, '$1.$2')
.replace(/\B(?=(\d{3})+(?!\d)\.?)/g, ",")
;
});
});
You can test it here: http://jsfiddle/fXrv2/
本文标签: javascriptFormat Amount field with comma and decimalStack Overflow
版权声明:本文标题:javascript - Format Amount field with comma and decimal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742409667a2469530.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论