user1531040
user1531040

Reputation: 2291

How to use OnKeyup in jquery

I want to use an event by leaving an input field in a matrix in JQuery. If the value is not a number you have to get a message "Value is not a number."

How do you do that? We asp and jquery.

    $(window).keyup(function (e){
        var sCurrentValue = "";
        var sValue = "";
        for (var i=1; i<$("#Rows").val(); i++) {
            for (var j=1; j<=7; j++) {
                sCurrentValue = $(document.getElementById("inputfld" + j + "_" + i)).val();

                // If Current value has data and the value is not a number then we stop the procedure
                if (sCurrentValue.length > 0 && isNaN(sCurrentValue)) {
                    sValue = sCurrentValue;
                    bValueIsNumber = false;
                    break;
                }
            }
        }

        if (!bValueIsNumber) {
            ShowMessage("Value [xxx] is not a number.");
            return;
        }
    }

Upvotes: 1

Views: 165

Answers (1)

user1853181
user1853181

Reputation: 785

Do not attach your listener to the whole document, attach it to the input elements only:

This will trigger on "enter" and "tab".

$("input").keyup(function(e) {
    if((e.which == 13) || (e.which == 9)) {
        var value = $(this).val();
        if(!validate(value)) {
            alert("validation failed");
            return;
        }
    }
}

This will trigger on "leaving the input field" as you requested:

$("input").blur(function() {
    var value = $(this).val();
    if(!validate(value)) {
        alert("validation failed");
        return;
    }
}

Upvotes: 1

Related Questions