Leader Leader - 10 months ago 55
Javascript Question

Fire alert when user typed specific word into input field

I want to fire an alert when user typed certain word into an input field... not when user clicked some submit button or anything... but immediately after the keyword is typed inside the field...

let's say the alert would fired immediately when "foobar" is typed into the following field:

<input id="password" name="password" />

is it possible to do that?

Answer Source

Yes, easily:

$(function() {
    var triggerWords = ['foobar', 'barfoo'];
    $('#password').keyup(function() {
        for (var i = 0; i < triggerWords.length; i++) {
            if ($(this).val().toLowerCase() == triggerWords[i]) {
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="password" name="password" />

This one handles multiple words. Here's a fiddle, too: http://jsfiddle.net/rnpykekx/