javascript - Format Amount field with comma and decimal - Stack Overflow

How would I format amount field with ma and decimal?I am able to get mas but this function does not al

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
Add a ment  | 

1 Answer 1

Reset to default 4

Is 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/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742409668a4438556.html

相关推荐

  • javascript - Format Amount field with comma and decimal - Stack Overflow

    How would I format amount field with ma and decimal?I am able to get mas but this function does not al

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信