Currently, I am using regex to validate the name field. Each time a key is pressed, a validation function is triggered. If the input matches the regex pattern, I need to empty out that specific character from the input field. However, when previewing the page in a browser, the matched key does not get emptied immediately; it waits for the next keypress event.
How can this issue be resolved? Any assistance would be greatly appreciated. Please take a look at the code snippet below for reference:
// JavaScript
$(".inputType").bind("keyup", function(e) {
var str = $(".inputType").val();
if (str !== undefined) {
for (i = 0; i < str.length; i++) {
if (!str.chatAt(i).match("^[a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]*$")) {
str = str.replace(str.charAt(i), "");
}
}
}
$(".inputType").val(str);
});