How can I correctly format currency using jquery?

前端 未结 7 1863
故里飘歌
故里飘歌 2020-12-13 01:16

I do not need a mask, but I need something that will format currency(in all browsers) and not allow for any letters or special char\'s to be typed. Thanks for the help

7条回答
  •  攒了一身酷
    2020-12-13 02:09

    Try regexp currency with jQuery (no plugin):

    $(document).ready(function(){
      $('#test').click(function() {
        TESTCURRENCY = $('#value').val().toString().match(/(?=[\s\d])(?:\s\.|\d+(?:[.]\d+)*)/gmi);
        if (TESTCURRENCY.length <= 1) {
          $('#valueshow').val(
            parseFloat(TESTCURRENCY.toString().match(/^\d+(?:\.\d{0,2})?/))
          );
        } else {
          $('#valueshow').val('Invalid a value!');
        }
      });
    });
    
    
    
    

    Edit: New check a value to valid/invalid

提交回复
热议问题