Asteroid
Asteroid

Reputation: 57

Accept only numbers in Textbox - jQuery

I need to implement a functionality for a textbox to allow only numbers. I have written the following code, but using Ctrl + V we are able to paste text. How can we prevent this.

$('.numeric-textbox').live('keypress', function (e) {
    if ((e.keyCode < 48) || (e.keyCode > 57)) {
        return false;
    }
});

Can someone suggest some solution. :)

Upvotes: 1

Views: 6641

Answers (2)

Sandeep Kumar
Sandeep Kumar

Reputation: 803

Option 1 : You can disable the CTRL +V refer here

Option 2 : Put the regex validation on lost focus or keyup to

Upvotes: 0

Nitin Varpe
Nitin Varpe

Reputation: 10704

Try

$('.numeric-textbox').bind("cut copy paste",function(e) {
          e.preventDefault();
      });

This will prevent cut copy paste event on your textbox

Upvotes: 1

Related Questions