I am trying to create a regex that only registers the alphabet on the standard English keyboard (a-z). However, the regex I currently have also registers the keys that start with letters... So ctrl, alt, shift, caps lock, etc. are recognized as valid keys pressed. I want to exclude these special keys however I can.
Here is what I have so far...
Register key down:
componentDidMount() {
document.addEventListener("keydown", this.handleKeyDown);
};
Validate if key is alphabetic:
handleKeyDown = (event) => {
const regex = /^[A-Za-z]+$/
// validate key press is alphabetic
if( regex.test(event.key) ) {
console.log(event.key);
} else {
console.log(event.key);
};
};