After using the replace to put a slash after the second character, I became unable to delete it.
1 Answer
Use event.key to check for the pressed key, and if the key is either Backspace or Delete return from the event handler without executing the insertion of the /:
const input = document.querySelector('input');
input.addEventListener('keydown', () => {
if(event.key === 'Backspace' || event.key === 'Delete') {
return;
}
if(input.value.length === 2) input.value += '/';
});
<input>


replace: you put the/back there.