Answer the question
In order to leave comments, you need to log in
How to remove spaces and letters from input?
Please tell me how to remove all spaces and letters on the fly from input using pure js .
Answer the question
In order to leave comments, you need to log in
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);
}
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question