anatp_123
anatp_123

Reputation: 1205

How can I get this function to work on old jquery?

Using it with jquery 1.6.4 it doesnt work , with the newer versions it works fine. Becasue I'm using a older jquery version I get error

TypeError: $(…).on is not a function

http://jsfiddle.net/w0jekdL6/18/

$(".amount").on("keyup", function(){
    var valid = /^\d{0,5}(\.\d{0,2})?$/.test(this.value),
        val = this.value;
    var pos = this.selectionStart - 1;

    if(!valid){
        if(typeof this.lastValid != "undefined") {
            this.value = this.lastValid
            this.setSelectionRange(pos, pos);
        } else {
            this.value = "";
        }
    } else {
        this.lastValid = val;
    }
});

Upvotes: 0

Views: 21

Answers (2)

Yotam Omer
Yotam Omer

Reputation: 15366

Use .keyup() instead of .on(): (according to the docs this will also work for the new versions of jQuery)

$(".amount").keyup(function(){
    var valid = /^\d{0,5}(\.\d{0,2})?$/.test(this.value),
        val = this.value;
    var pos = this.selectionStart - 1;

    if(!valid){
        if(typeof this.lastValid != "undefined") {
            this.value = this.lastValid
            this.setSelectionRange(pos, pos);
        } else {
            this.value = "";
        }
    } else {
        this.lastValid = val;
    }
});

jQuery's .on() was only added in version 1.7

Upvotes: 1

mhodges
mhodges

Reputation: 11116

I believe this will work - see jQuery Docs

$(".amount").keyup(function () {...});

Upvotes: 0

Related Questions