How to disable backspace in jquery-inputmask if remainig number is 0?

49 views Asked by At

The following input field doesn't allow numbers start with 0, but it can be beat by typing e.g. 10 or 20 or 100 or 2390. The important thing is that the number ends with 0, and the user can delete all the numbers before getting 0. Is there a way do avoid that? For example, when the remaining number is 0, it doesn't allow delete it.

$('#currency').inputmask({
  regex: '^[1-9][0-9]*([,.][0-9]{1,2})?$',
  rightAlign: false,
  placeholder: '',
  min: 1,
  inputmode: 'decimal',
  casing: function (elem) {
    if(elem == ',') return '.';
    return elem;
  }
});

Codepen: https://codepen.io/levipadre/pen/gOQMKXW?editors=1010

1

There are 1 answers

2
Parthiv On
$('#currency').inputmask({
  regex: '^[1-9][0-9]*([,.][0-9]{1,2})?$',
  rightAlign: false,
  placeholder: '',
  min: 1,
  inputmode: 'decimal',
  casing: function (elem) {
    if(elem == ',') return '.';
    return elem;
  }
}).on('keydown', function (e) {
  if ($(this).val() === '0' && e.keyCode === 8) {
    e.preventDefault();
  }
});

Using key down in inputmask we can disable backspace by matching the value of the input field