I want to prevent 0 as the first character. But when I focusout and focusin again, then I add "2" in the first, it still run, I want should not run.
Here is my HTML
<input type="number" id="test">
My JS
$('#test').keypress(function(evt) {
if (evt.which == "0".charCodeAt(0) && $(this).val().trim() == "") {
return false;
}
});
Anybody help or suggest what should I do? Thank you.
"0"
? (So"02"
would not be allowed but"20"
would be allowed?) Because if so you can't do that by testing just the current keypress because the user may type the"2"
then move the cursor in front of it and type the"0"
. (Also they may edit the field without using the keyboard, i.e., by pasting or drag'n'drop.) – Cornerstone