Phillip Senn
Phillip Senn

Reputation: 47643

jQuery numeric input mask

I'm trying to do a simple integer numeric input mask without having to include an entire plugin like Digital Bush's masked input.

Here's the code I've "repurposed" from another stackoverflow question:

$('input[name=Qty]').keyup(function() {
    var theEvent = evt || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode( key );
    var regex = '[0-9]'; /* Use |\. to include a decimal */
    if( !regex.test(key) ) {
     theEvent.returnValue = false;
     theEvent.preventDefault();
    }
});

The error is on the first line, where it doesn't know what evt is.
I think I need to use $(this) something.

Upvotes: 1

Views: 7531

Answers (2)

Phillip Senn
Phillip Senn

Reputation: 47643

I've found this as an alternative:

$('input[name=Qty]').bind('keyup blur',function(){ 
    var myValue = $(this).val();
    $(this).val( myValue.replace(/[^0-9]/g,'') );
});

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382899

Missing evt in function parameter:

$('input[name=Qty]').keyup(function(evt) {
                                     ^

Upvotes: 4

Related Questions