set-decimal-input.js 718 B

1234567891011121314151617181920212223
  1. $(function () {
  2. const regExp = /[0-9\.\,]/;
  3. $(document).on('keydown keyup', 'input.decimal-input', function (e) {
  4. const value = String.fromCharCode(e.which) || e.key;
  5. // Only numbers, dots and commas
  6. if (!regExp.test(value)
  7. && e.which != 188 // ,
  8. && e.which != 190 // .
  9. && e.which != 8 // backspace
  10. && e.which != 46 // delete
  11. && (e.which < 37 // arrow keys
  12. || e.which > 40)) {
  13. e.preventDefault();
  14. return false;
  15. }
  16. $(this).val(function (index, value) {
  17. value = value.replace(/,/g, '');
  18. return numberWithCommas(value);
  19. });
  20. });
  21. });