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

3
  • 1
    What version of jQuery are you using? live() is deprecated in 1.7, and removed entirely in 1.9 (replaced with on()). Commented Nov 21, 2013 at 11:20
  • @ASTEROID why are you using 1.6.2? any reason not to update and pull the latest /1/ version? Commented Nov 21, 2013 at 11:35
  • Old Application. Requirement.. Standard.. Whatever they call. Commented Nov 21, 2013 at 11:45

2 Answers 2

1

Try

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

This will prevent cut copy paste event on your textbox

Sign up to request clarification or add additional context in comments.

3 Comments

@A.Wolff If u want you can remove it
I would also disallow to paste only numbers?
@Nitinvarpe Now I can enter anything in the textbox.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.