6

I have got a task to disable the textbox from pasting.Only the alphabetic characters must be allow to paste. My code is

<b>Name</b>
<input type="text" id="name" onpaste="return false"/>

By giving onpaste="return false" no values are pasted. How can I solve this problem?

Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84

4 Answers4

7

Try this code

$(".alphabetOnly").bind('paste', function(e) {
  var self = this;
  setTimeout(function(e) {
    var val = $(self).val();
    if (val != '0') {
      if (val.match(/^[0-9]+$/) != null) {
        $(".alphabetOnly").val("");
      }
      $(this).val(val);
    }
  }, 0);
});

I have updated the code here

Bhuwan
  • 16,525
  • 5
  • 34
  • 57
Okky
  • 10,338
  • 15
  • 75
  • 122
2

Made some mistakes before, this is working:

$('#name').bind('paste', function(){
    var self = this;
    setTimeout(function() {
        if(!/^[a-zA-Z]+$/.test($(self).val()))
            $(self).val('');
    }, 0);    
});

You have to remove the onpaste="return false" from your html!

A working example can be found here: JS Fiddle

Rob
  • 4,927
  • 12
  • 49
  • 54
2

This code restrict pasting of other characters apart from numbers in the input field.On pasting a value to an input field, I'm testing the value with the regex, if the condition is true, the value will be set, or else i'm emptying the input value.In my case i had to restrict other characters apart from numbers, you can change the regex based on your requirement

$(".numbersOnly").bind('paste', function(e) {
        var self = this;
        setTimeout(function(e) {
            var val = $(self).val();
            if (val != '0') {
                var regx = new RegExp(/^[0-9]+$/);
                if (!regx.test(val)) {
                    $(".numbersOnly").val("");
                }
                $(this).val(val);
            }
        }, 0);
    });
  • A self explanatory answer is just awesome. So can you please explain a bit about your answer and how this code works.! – Rahul Sharma Jan 12 '18 at 05:05
  • This code restrict pasting of other characters apart from numbers in the input field.On pasting a value to an input field,i'm testing the value with the regex,if the condition is true,the value will be set,or else i'm emptying the input value.In my case i had to restrict other characters apart from numbers,you can change the regex based on your requirement – Abhilash Sringar Jan 12 '18 at 06:04
0

Place a call to a function inside of your onpaste value. Then, inside of that function, weed-out the non-alphabetic characters.

See this question for more details.

Community
  • 1
  • 1
WEFX
  • 8,298
  • 8
  • 66
  • 102
  • How would you go about weeding out the non alphabetic characters in the onpaste event. The `paste` event is fired before the input value changed. You would need a Timeout or something else to modify the input's value – Moritz Roessler Mar 27 '13 at 13:45
  • Correct, a timeout is used in the referenced "this question" link. – WEFX Mar 27 '13 at 19:53