JSFiddle - React, Tailwind, and code Playground
HTML
<p>Please press any key.</p>
<input type="text" />
JavaScript
function showKeyPress(evt) {
console.log("onkeypress handler: \n" + "keyCode property: " + evt.keyCode + "\n" + "which property: " + evt.which + "\n" + "charCode property: " + evt.charCode + "\n" + "Character Key Pressed: " + String.fromCharCode(evt.charCode) + "\n");
}
$('input').bind('keypress', function(event) {
showKeyPress(event);
var regex = new RegExp("^[a-zA-Z0-9]+$");
var code = event.charCode === 0 ? event.which : event.charCode;
var key = String.fromCharCode(code);
if (!regex.test(key) && code !== 8) {
event.preventDefault();
return false;
}
});