How to allow only a particular word in the textbox using jquery?

This question already has an answer here:

My requirement is I have to allow the user to enter only '@' keyword. if the user enter the other keyword it should not display anything in the textbox.

<input type="text" />
document.getElementById('txtSearch').onkeypress = function (e) {
    if (e.keyCode === 64 ) {
        alert('welcome');
        return false;
    }
};

Answers 1

  • You can restrict any word using the following code.

    $("#IDofTextInput").on("keyup", function(){
        if(!("@".startsWith($("#IDofTextInput").val())))
      {
        $("#IDofTextInput").val($("#IDofTextInput").val().substring(0, $("#IDofTextInput").val().length-1));
      }
    });
    

Related Articles