The following blocks out all chars which are not either alphabetic or backspace (\b
), but you can see that the Backspace doesn't work and is also filtered out. Why is that?
$('#field').keydown(function(e) {
if (!/^[A-Za-z\b]$/i.test(e.key)) {
e.preventDefault();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="field" />