You have to do 2 things:

  1. Bind paste event (as is already mentioned above)
  2. Fix your regex that allows characters in the range between @ and _ (all letters and some characters like ^, [... because you did not escape the hyphen, and it creates a range [@-_], which is valid (that is why you are not getting any error), but that is not what you expect.

Here is the fixed code:

$('input').bind('keypress paste', function (event) {
var regex = /^[a-zA-Z0-9%()#@_& -]+$/;
var key = String.fromCharCode(event.charCode || event.which);
if (!regex.test(key)) {
   event.preventDefault();
   return false;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="txt1"/>

Answer from Wiktor Stribiżew on Stack Overflow
Discussions

javascript - Copy/paste validation on input field and restrict character - Stack Overflow
I am working on validation of an input control when the user copy paste some value in it On pasting to this input I want to strip out as below: Input can start with underscore or an alphabet Input More on stackoverflow.com
🌐 stackoverflow.com
November 27, 2019
javascript - Client side form validation for Copy/Pastes via Right-Click - Stack Overflow
I'm using the following line (Struts1 syntax) to display a text field and allow some client side checks via Javascript. More on stackoverflow.com
🌐 stackoverflow.com
Limit chars in textarea: problem with copy-paste - JavaScript - SitePoint Forums | Web Development & Design Community
I need to limit the chars typed in a textarea to 160 (size of an SMS :p) It works fine - as long as no one is copy-pasting text to it. Then my lil JS fails boohoo. So, could anyone help me out? Should I nail this to some… More on sitepoint.com
🌐 sitepoint.com
0
January 21, 2003
html - how to detect copy and paste in javascript? - Stack Overflow
I have two fields, one is emailid and another is password in my form. I want to prevent the user from pasting into those fields. They should be forced to enter manually, like Google Forms. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Code2night
code2night.com › javascript › input-validation-to-stop-backslash-on-keypress-and-copy-paste
Input validation to stop backSlash on keypress and copy paste | Code2night.com
This function retrieves the pasted data, removes any backslashes, and updates the input field accordingly. This ensures that the user cannot paste invalid characters into the textbox.
🌐
Code Beautify
codebeautify.org › jsvalidate
Best JavaScript Validator Online
JS Validator uses JavaScript libs for validating and presenting warnings and errors. It process and validates js in a browser environment. Just Paste your JS code and click Validate JS.
🌐
Human Who Codes
humanwhocodes.com › blog › 2023 › 07 › disabling-paste-textboxes-security
Disabling paste in textboxes is not a security feature - Human Who Codes
July 3, 2023 - Right-click on the textbox and click “Inspect”. If you see onpaste="return false" in the DOM representation of the textbox, right-click on it and click “Edit as HTML” to remove the attribute.
🌐
W3Schools
w3schools.com › jsref › event_onpaste.asp
onpaste Event
It is only possible to paste something into an input field.
🌐
DEV Community
dev.to › clairecodes › how-to-prevent-pasting-into-input-fields-nn
How to prevent pasting into input fields - DEV Community
February 1, 2019 - A short JavaScript snippet to stop users from pasting content into form fields. Tagged with javascript, html, webdev.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 59075019 › copy-paste-validation-on-input-field-and-restrict-character
javascript - Copy/paste validation on input field and restrict character - Stack Overflow
November 27, 2019 - @HostListener('paste', ['$event']) blockPaste(event: KeyboardEvent) { this.stripInput(event); } stripInput(event) { setTimeout(() => { this.el.nativeElement.value = this.el.nativeElement.value.replace(/^[^A-Za-z_]+/g, '').replace(/[^0-9A-Za-z_]+/g, '').replace(/\s/g, ''); event.preventDefault(); }, 100); }
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Element › paste_event
Element: paste event - Web APIs | MDN
const target = document.querySelector("div.target"); target.addEventListener("paste", (event) => { event.preventDefault(); let paste = (event.clipboardData || window.clipboardData).getData("text"); paste = paste.toUpperCase(); const selection = window.getSelection(); if (!selection.rangeCount) return; selection.deleteFromDocument(); selection.getRangeAt(0).insertNode(document.createTextNode(paste)); selection.collapseToEnd(); });
🌐
Stack Overflow
stackoverflow.com › questions › 31121871 › client-side-form-validation-for-copy-pastes-via-right-click
javascript - Client side form validation for Copy/Pastes via Right-Click - Stack Overflow
I'm using the following line (Struts1 syntax) to display a text field and allow some client side checks via Javascript. <html:text styleId="myField" property="myProperty" onkeyup="function()" /> My intention is for a message to appear and a dropdown to disable whenever there is text entered into the form field (regardless of content). The onkeyup attribute works fine for all cases except for when the user pastes in text using mouse right-click.
🌐
SitePoint
sitepoint.com › javascript
Limit chars in textarea: problem with copy-paste - JavaScript - SitePoint Forums | Web Development & Design Community
January 21, 2003 - Otherwise, validation should be done server side. ... <script type=“text/javascript”> function LimitText(fieldObj,maxChars) { var result = true; if (fieldObj.value.length > maxChars) { result = false; } if (window.event) { window.event.returnValue = result; } return result; } </script> <textarea name=“myTextArea” OnKeyPress=“LimitText(this,4)”></textarea>
Top answer
1 of 5
15

2020 update

There are copy and paste events you can use to prevent these actions, or to modify the data being copied or pasted. (see the links for browser support)

Copy<input type="text" onpaste="return false">
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Or the longer javascript version:

Copyconst elem = document.getElementById('nopaste');

elem.addEventListener('paste', (event) => {
  event.preventDefault();
});
Copy<input type="text" placeholder="can paste"><br>
<input type="text" id="nopaste" placeholder="can not paste">
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 5
12

You could disable ctrl+v combination and right click as well.

for IE, you may tap into following event handlers:

Copyonpaste="return false;" 
oncut="return false;" 
oncontextmenu="return false;" 
oncopy="return false;".

Here is a workaround for all browsers:

Copyfunction noCTRL(e) {
      var code = (document.all) ? event.keyCode : e.which;
      var ctrl = (document.all) ? event.ctrlKey : e.modifiers & Event.CONTROL_MASK;
      var msg = "Sorry, this functionality is disabled.";
      if (document.all) {
        if (ctrl && code == 86) {
          //CTRL+V
          alert(msg);
          window.event.returnValue = false;
        } else if (ctrl && code == 67) { 
         //CTRL+C (Copy)
          alert(msg);
          window.event.returnValue = false;
        }
      } else {
        if (ctrl == 2) {
          //CTRL key
          alert(msg);
          return false;
        }
      }
    }

In HTML section, your fields would look like:

CopyEmail :<input name="email" type="text" value=""/><br/>
Password :<input name="password" type="password" value=""/><br/>
Confirm Email :<input name="email" type="text" value="" onkeydown="return noCTRL(event)"/>    
Confirm Password :<input name="password" type="password" value="" onkeydown="return noCTRL(event)"/>

I don't think user can copy password fields if input type is password

Hope this helps.

Note:

  1. Disabling JavaScript in browser will let users do whatever they want
  2. Always Keep this in mind: respect user's freedom.
🌐
ValidateJavaScript
validatejavascript.com
ValidateJavaScript - Online Tool to Find & Fix JavaScript Errors
ValidateJavaScript is an online validating (or linting) tool that will automatically find basic errors and help prevent potentially destructive bugs in JavaScript and JSX (React.js) code. Copy and paste or directly input your code into the editor above, click the "Find & Fix Errors" button, and ...
🌐
Site24x7
site24x7.com › tools › javascript-validator.html
Javascript Validator | Validate Online - Site24x7 Tools
Simply paste your code, validate it and copy or download the validated code. ... Are you an application developer? Try Site24x7 APM Insight to get deep visibility of your applications performance problems. ... Mail Delivery Monitoring. Monitor your SMTP server.
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 105670 › validation-paste-ctrl-v
javascript - Validation - Paste (Ctrl V) | DaniWeb
January 22, 2008 - Good instinct from @MidiMagic, @digital-ether, and @s.o.s: you cannot reliably stop every paste path, but you can control what ends up in the field and validate it again on submit. Blocking Ctrl+V like @hooray suggested misses menu paste, long-press on mobile, IME, and more.