Overview
Would you like to prevent copy and paste in your forms? You may want to prevent your visitors from copying and pasting anything into your form fields.
In this article, we’ll show you how you can use a custom snippet to prevent your visitors from pasting anything into your form fields.
Note: If there are any options on the Advanced tab enabled for the field, this snippet may not work correctly for you. Examples are text or character limits on the Single Line Text or Paragraph form fields.
Adding the Snippet
Just add this snippet to your site. If you need any assistance with where and how to add snippets to your site, please check out this tutorial.
/**
* Prevent visitors from copying and pasting in your form fields
*
* @link https://wpforms.com/developers/how-to-prevent-copy-and-paste-inside-your-form/
*/
function wpf_dev_prevent_copy_paste( ) {
?>
<script type="text/javascript">
jQuery(document).ready(function () {
// Update the form ID to match your own form ID
jQuery( 'form#wpforms-form-2189' ).on( 'copy paste', function (e) { e.preventDefault(); } );
});
</script>
<?php
}
add_action( 'wpforms_wp_footer_end', 'wpf_dev_prevent_copy_paste', 30 );
In the snippet, we’re only processing this snippet on the form with an ID 2189
. You’ll need to update this form ID to match your form’s ID. If you need assistance finding this ID, please check out this tutorial.
When a visitor comes to your site and tries to paste into your form fields, the fields will remain empty until the visitor physically types into the field.
Frequently Asked Questions
These are answers to some of the top questions about disabling copy and paste functionality in WPForms.
What if I wanted to do this for all of my WPForms?
If you’d like to force this on all of your WPForms, use this snippet instead.
/**
* Prevent visitors from copying and pasting in your form fields
*
* @link https://wpforms.com/developers/hhow-to-prevent-copy-and-paste-inside-your-form/
*/
function wpf_dev_prevent_copy_paste( ) {
?>
<script type="text/javascript">
jQuery(document).ready(function () {
// Update the form ID to match your own form ID
jQuery( '.wpforms-container' ).on( 'copy paste', function (e) { e.preventDefault(); } );
});
</script>
<?php
}
add_action( 'wpforms_wp_footer_end', 'wpf_dev_prevent_copy_paste', 30 );
Can I prevent a copy and paste in the Rich Text Editor field?
Using the snippet above, when you are on the Text tab of the Rich Text Editor field, it will automatically be disabled with the script. However, when on the Visual tab, since this is loaded via an iFrame, the snippet to prevent the copy and paste function doesn’t currently work.
That’s it! You’ve successfully stopped users from performing the copy or paste function in your form fields.
Next, would you like to capitalize form inputs on your site? Check our tutorial to learn how to capitalize form field inputs in WPForms.
Related
Action Reference: wpforms_wp_footer_end