Skip to content

Instantly share code, notes, and snippets.

@benerdin
Created July 17, 2014 14:06
Show Gist options
  • Save benerdin/1d7a8b27c26eb0173a42 to your computer and use it in GitHub Desktop.
Save benerdin/1d7a8b27c26eb0173a42 to your computer and use it in GitHub Desktop.
How to prevent pasting into a text field using jQuery.
$(document).ready(function() {
$('#elementId')
.keypress(function(e) {
// Prevent users from being able to type into the field.
e.preventDefault();
})
.keydown(function (e) {
// Prevent users from being able to type into the field.
e.preventDefault();
})
.keyup(function (e) {
// Prevent users from being able to type into the field.
e.preventDefault();
})
.bind('contextmenu', function(e) {
// Prevent users from being able to right-click and paste into the field.
e.preventDefault();
})
;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment