I’ve written simple JavaScript mask that allows you to write only numbers to input. But the thing is, when you press arrow key, the caret will move back to the end of the line.
How can I ignore arrow keys, or other meta keys?
Here’s my mask:
function applyMaskInteger(elementId) {
var e = document.getElementById(elementId);
e.value = e.value.replace(/[^0-9]/g,"");
}
PS: I like jQuery, but please answer only with plain JavaScript … (we don’t use jQuery on this project)
I guess it’s a regular
<input type="text">.You can move the caret (at least in modern browsers) by altering the values of its
selectionStartandselectionEndproperties.