1

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. :)

Asteroid
  • 57
  • 1
  • 11
  • 1
    What version of jQuery are you using? [`live()`](http://api.jquery.com/live/) is deprecated in 1.7, and removed *entirely* in 1.9 (replaced with [`on()`](http://api.jquery.com/on/)). – David Thomas Nov 21 '13 at 11:20
  • @ASTEROID why are you using 1.6.2? any reason not to update and pull the latest `/1/` version? – Roko C. Buljan Nov 21 '13 at 11:35
  • Old Application. Requirement.. Standard.. Whatever they call. – Asteroid Nov 21 '13 at 11:45

2 Answers2

1

Try

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

This will prevent cut copy paste event on your textbox

Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60
0

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

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

Community
  • 1
  • 1
Sandeep Kumar
  • 783
  • 1
  • 5
  • 13