- SPLessons

Disable Copy and Paste of text in Textbox using Jquery

Home > > Tutorial
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Disable Copy and Paste of text in Textbox using Jquery

Disable Copy and Paste of text in Textbox using Jquery

  The following example shows when client want to restrict the users from copy/paste in textbox.   I have a textbox 'loanamount'. Copy/paste can do in two ways i.e. Ctrl+V (or) with mouse right click. Now I want restrict the users from copy/paste in this textbox. For this copy below code into your page.     [javascript] <script type="text/javascript"> $(function () { // on Key Press event restrict to enter characters $(".numericOnly").bind('keypress', function (e) { if (e.keyCode == '9' || e.keyCode == '16') { return; } var code; if (e.keyCode) code = e.keyCode; else if (e.which) code = e.which; if (e.which == 46) return false; if (code == 8 || code == 46) return true; if (code < 48 || code > 57) return false; } ); // this event will fire on (Ctrl+v) and it will disable $(".numericOnly").bind("paste", function (e) { e.preventDefault(); }); // this event will fire, when your trying to paste with mouse right click and it will disable $(".numericOnly").bind('mouseenter', function (e) { var val = $(this).val(); if (val != '0') { val = val.replace(/[^0-9]+/g, "") $(this).val(val); } }); }); </script> [/javascript] Add below HTML content this in your page body [html] Loan Amount: <input type="text" class="numericOnly" /> [/html]