"Interception" of user input in a text field and its removal

I have a text box in which I would like to do some validation. At the moment I have this code:

function updateChanger() {

    // Validate input
    var likeMessage = validateInput($("#like").val());

    alert(likeMessage);
}

function validateInput(input) {
    input = input.replace(/[^a-zA-Z0-9:\(\/\)\s\.,!~]/g, "");
    return input;
}

This successfully eliminates the unwanted characters in the likeMessage variable, but the character is still entered in the text box. I would like to stop this.

I know this will be relevant $("#like").val(), but the only thing I can come up with is just chopping up the final character from the value of the text field. Would that be enough?

Thanks for any help!

+3
source share
1 answer

, keyup , :

$("#like").keyup(function() {
  $(this).val($(this).val().replace(/[^a-zA-Z0-9:\(\/\)\s\.,!~]/g, ""));
});

, , .

+5

Source: https://habr.com/ru/post/1748947/


All Articles