You have to do 2 things:
- Bind
pasteevent (as is already mentioned above) - 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 OverflowYou have to do 2 things:
- Bind
pasteevent (as is already mentioned above) - 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"/>
$("#textareaid").bind("paste", function(e){
// access the clipboard using the api
var pastedData = e.originalEvent.clipboardData.getData('text');
alert(pastedData);
} );
or you can try this way JQuery paste
There is one more way out, that you can do it so with onChange or focusOut events on textbox
javascript - Copy/paste validation on input field and restrict character - Stack Overflow
javascript - Client side form validation for Copy/Pastes via Right-Click - Stack Overflow
Limit chars in textarea: problem with copy-paste - JavaScript - SitePoint Forums | Web Development & Design Community
html - how to detect copy and paste in javascript? - Stack Overflow
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
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:
- Disabling JavaScript in browser will let users do whatever they want
- Always Keep this in mind: respect user's freedom.
You better off with something like:
$('input').bind('keyup', function(e) {
this.value = this.value.replace(/[^0-9]/g,'');
});
Or you can also use the change event. In this case no matter how the data gets into the field it will be validated (and non numeric input removed).
Keep a record of the last keycode pressed. Since you're using onkeydown, a cmd-v would show up as an event with keycode 224 (cmd) and then an event with keycode 86 (v). If the previous key matches cmd and the latter v, allow it through.
(you would probably check for ctrl for Windows/Linux pasters as well)
Don't do it. Don't mess with the user's browser. By Copy + Pasting into an E-Mail confirmation field, the user accepts responsibility over what they type. If they are dumb enough to copy + paste a faulty address (it has happened to me) then it's their own damn fault.
If you want to make sure that the E-Mail confirmation works out, have the user check their E-Mail while your site waits ("Please open your webmail program in a new window"). Show the E-Mail address in big fat letters ("The confirmation E-Mail was sent to.... made an error? CLick here to change).
Even better, if you can, let the user have some kind of limited access without confirming. That way, they can log in straight away and you improve your chances to keep in touch with the visitor even if the confirmation mail is blocked due to other reasons (e.g. spam filters).
Add a class of 'disablecopypaste' to the inputs you want to disable the copy paste functionality on and add this jQuery script
$(document).ready(function () {
$('input.disablecopypaste').bind('copy paste', function (e) {
e.preventDefault();
});
});
First option: To add the same class to both inputs and use document.getElementsByClassName('some-class')
Second option: To use a single input and on (keyup) call the validation function which will check if the user input matches the regex you need
This appears to be the answer:
punctuated.setCustomValidity(real.validationMessage);
I just bound that to the page load, and a click and keyup event for the punctuated input, and it seems to work. So when the user types stuff into the punctuated input, it gets copied over to the real input and validated using the html attributes, and then the click and keyup events pass the validation message to the punctuated input.
So my understanding of regex is only the basics, I am working on a practice project and trying to understand how the regex email will match the email string drives me insane, so I just copy-paste, I feel for a beginner like me I should be knowing only the basics of how it works, when and how to use it?
copy pasting isn't bad until and unless you are blinding doing it. I many time copy paste code from UI library. It make my life much easy
Copy pasting email regex is okay. You should probably know the basics of regex, but that isn't going to come from that email regex, it will come from simple regex that you end up needing to build yourself. Like I recently discovered that number inputs in firefox still allow you to enter text so I had to write a regex that only accepted numbers and number-related punctuation (plus special characters like backspace). That kind of simple thing will give you a basis in regex that you can rely on, but email regex is just something you can copy.