I delete unnecessary characters from the input field as follows:

$(document).on('input','#num',function() { this.value = this.value.replace(/[^\d\.]/ig,''); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id="num"> 

But so any change in the field resets the cursor to the end. Here you can come up with something or need a different approach?

  • type="number" - Grundy
  • @Grundy, And if validation is needed for something else? - user207618
  • @Other, obviously then it will be necessary to look separately - Grundy

1 answer 1

You insert a new value, here and throws.
You can be confused with the definition of the position of the cursor, get a tambourine, but the most correct thing is to simply define the left character when entering and not to let it enter:

 $(document).on('keydown','#num',function(e) { // Проверка на длину необходима для того, чтобы допускать действие спец. клавиш типа стрелок if(e.key.length === 1 && /\D/.test(e.key)) return false; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id="num">