How can I change this so that the input only allows the characters A-Z, a-z, 0-9 while typing, without using a regular expression?
$(function() {
$("#user").keyPress(function(event) {
alert(event.which);
});
});
input {
padding: 5px 8px;
font-size: 15px;
outline: none;
color: #333;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<input type="text" name="" id="user" />
Assuming you also want to accept spaces:
$("#user").keypress(function(event){
var ew = event.which;
if(ew == 32)
return true;
if(48 <= ew && ew <= 57)
return true;
if(65 <= ew && ew <= 90)
return true;
if(97 <= ew && ew <= 122)
return true;
return false;
});
If you don't want to accept spaces then remove the if(ew == 32) return true;