There is a script that limits the number of introductory characters to 3 and only numbers are allowed.
But the bug is that after I entered 3 characters, I removed the focus from the input, and then once again took the input into focus, then the 3rd character is removed.
How to fix the script so that you can enter ONLY numbers, DO NOT enter the first 0, limit the input to only 3 characters?
$('body').on('click keypress', '.btn-input', function(key) { $this = $(this); $maxLen = 2; if (key.charCode < 48 || key.charCode > 57) return false; if ($this.val().length > $maxLen) $this.val($this.val().substr(0, $maxLen)); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="btn-input order-count" type="text" >
keypresswithkeyup, and the stringif (key.charCode < 48 || key.charCode > 57) return false;put it at the very end, - Alexey Shimansky$maxLen = 2;leave it like this:if ($this.val().length > $maxLen) return false; if (key.charCode < 48 || key.charCode > 57) return false;if ($this.val().length > $maxLen) return false; if (key.charCode < 48 || key.charCode > 57) return false;........... jsfiddle.net/4gym7k8h/1 - Alexey Shimanskyif ($this.val().length == 0 && key.charCode == 48) return false;- Alexey Shimansky