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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With