Reputation: 159
I have a fiddle here that removes the last character from the input when the user types .
However it seems .length
ignores the decimal when there are no numbers after it.
For example: currently when the user types 20.
, it will be cut to 2
instead of 20
.
How can I detect this and remove the decimal once the user types it?
Upvotes: 0
Views: 184
Reputation: 52210
I think your requirement is to ignore it when a user types the period.
So all you need is this (notice the event is on keydown
and not keyup
):
$('input').on('keydown', function(e) {
if (e.keyCode == 190 || e.keyCode == 110){
e.preventDefault();
}
});
Or, even easier, attribute the input so it only takes whole numbers:
<input type="number" min="0" step="1"/>
Upvotes: 1