input.addEventListener('input', event => {
const element = event.target;
const currentValue = element.value;
const preparedValue = currentValue.replace(/\D/g, '');
const start = element.selectionStart;
const end = element.selectionEnd;
if (preparedValue !== currentValue) {
element.value = preparedValue;
element.dispatchEvent(new Event('change', { bubbles: true }));
if (event.inputType === 'insertText') {
const offset = event.data.length;
element.setSelectionRange(start - offset, end - offset);
}
}
});
Для Vue
https://codesandbox.io/s/qna-q1013532-zf2xk