Исключить последний символ в событии

Есть событие типа $(".class").on("input", ...

В нем есть условие,

var balance = 10000;
$(".sumOut").on("input", function() {
  if ($.isNumeric(this.value)) {
    console.log(this.value); // Будет показывать те самые 150001 150002 и т.п., что ломает нижние условия..
    if (this.value.length >= 4) {

      if (this.value > 12e3) 
      {
        $('.sumOut').addClass('error_input');
        $('.error_text.s').text('Некорректная сумма');
        $('.error_text.s').fadeIn(350);
        $(this).val(12e3);


      } else if (this.value > balance) {
        $('.sumOut').addClass('error_input');
        $('.error_text.s').text('Недостаточно средств, поэтому установлена сумма равная вашему балансу');
        $('.error_text.s').fadeIn(350);
        $(this).val(balance);

      }

    }
  } else this.value = this.value.replace(/[^\d]/g, "");
})
.error_text {
  color: red;
  display: none;
}

.error_input {
  border: 1px solid red;
}

input {
  outline: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="sumOut">
<p class="error_text s"></p>

И все вроде бы хорошо, но если продолжать тыкать, то в поле ничего меняться не будет. Т.е будет число 15000, однако смотря в console.log(this.value) можно увидеть, как появляются числа 150001, 150005, 150008 и т.п (зависит от нажатой кнопки). Это ломает идущие ниже условия..

Как это пофиксить ?


Ответы (1 шт):

Автор решения: NoSkill

var balance = 10000;
var maxamount = 12000;

  $(".sumOut").on("input", function() {

    $('.error_text.s').text('');
    $('.error_text.s').hide();

    if ($.isNumeric(this.value)) {
    
      if (this.value > maxamount) {
        $('.sumOut').addClass('error_input');
        $('.error_text.s').text('Некорректная сумма (максимум ' + maxamount + ')');
        $('.error_text.s').fadeIn(350);
        $(this).val(maxamount);
      } 

      if (this.value > balance) {
        $('.sumOut').addClass('error_input');
        $('.error_text.s').text('Недостаточно средств, поэтому установлена сумма равная вашему балансу');
        $('.error_text.s').fadeIn(350);
        $(this).val(balance);
      }
   
    } else {
      this.value = this.value.replace(/[^\d]/g, "");
    }
  });
.error_text {
  color: red;
  display: none;
}

.error_input {
  border: 1px solid red;
}

input {
  outline: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="sumOut">
<p class="error_text s"></p>

→ Ссылка