Stefan Houtzager
Programmer
Hi,
I'm new here. I want to prevent the input of for example an é character with alt130 or strange chars like ☻ (alt2) in a text input. This seems difficult ( I could not do it with preventdefault in a keydown or keyup event. Tips?
Thanks, Stefan.
Edit:
Found a solution, works in the newest version of chrome at least
element.onkeydown = e => {
element.altKeyDown = e.altKey;
element.oldValue = element.value;
etc.
element.oninput = () => {
if (element.altKeyDown) {
let cursorPosition = element.selectionStart - 1;
element.value = element.oldValue;
element.selectionStart = cursorPosition;
element.selectionEnd = cursorPosition;
}
}
I'm new here. I want to prevent the input of for example an é character with alt130 or strange chars like ☻ (alt2) in a text input. This seems difficult ( I could not do it with preventdefault in a keydown or keyup event. Tips?
Thanks, Stefan.
Edit:
Found a solution, works in the newest version of chrome at least
element.onkeydown = e => {
element.altKeyDown = e.altKey;
element.oldValue = element.value;
etc.
element.oninput = () => {
if (element.altKeyDown) {
let cursorPosition = element.selectionStart - 1;
element.value = element.oldValue;
element.selectionStart = cursorPosition;
element.selectionEnd = cursorPosition;
}
}