JSFiddle - React, Tailwind, and code Playground
HTML
<input type="text" placeholder="valid:abc" autofocus="true" />
CSS
input {
padding:2%;
box-shadow:1px 1px 1px rgba(0, 0, 0, 0.5);
border:0;
outline:0;
border:1px solid lightgrey;
}
JavaScript
input = document.querySelector("input");
restrictChars(input, "abc");
function restrictChars(input, chars) {
input.addEventListener("keypress", function (e) {
e.preventDefault();
var character = String.fromCharCode(e.keyCode);
var isValid = chars.indexOf(character) != -1;
if (isValid) {
input.value += character;
}
});
}