Reputation: 57
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
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
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