JSFiddle - React, Tailwind, and code Playground
JavaScript
function isKeypressCharValid(e, chars) {
e = e || window.event;
// Allow delete, escape, tab and enter
if (/^(8|9|13|27)$/.test("" + e.keyCode)) {
return true;
}
var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
var charTyped = String.fromCharCode(charCode);
return chars.indexOf(charTyped) > -1;
}
var textBox = document.getElementById("test");
textBox.onkeypress = function(evt) {
if (!isKeypressCharValid(evt, "abc123")) {
return false;
}
};