Disable Cut,Copy and Paste using jQuery

 How to prevent cut, copy, paste in text box.

 

 

prevent-copy-paste-by-coding-zon

 

Generally while registering in website, user forced to type manually some data. Ex: confirm email/confirm password. It is because to ensure user entered correct data.

This tutorial shows how to stop cut, copy, pasting data in a text field while entering his details such as email/password.
The below example shows how to prevent cut, copy, and pasting in confirm email input field. This is implemented using jQuery.

With the help of jQuery bind() function, it can be achieved. In the bind() function, you have to specify the cut, copy, and paste events that are fired when a user attempts to cut, copy, or paste in a text field. When it occurs, it shows an alert message to the user.

 

TOP

 

index.php

<html>
<head>
<title>Codingzon Tutorials</title>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
 
<script>

$(document).ready(function(){
    $("#confirmEmail").bind("cut copy paste",function(e) {
        e.preventDefault();   // prevents to submit to server(PHP)
        alert("copy,paste is disabled!"); 
    });
});

</script>
</head>
<body>
<center>

<br /><br /><br /><br /> 
 <?php

if(isset($_POST['submit']))
  {  
    
   $email= $_POST['email'];
   $confirmEmail= $_POST['confirmEmail'];

	if( $email == $confirmEmail)
	{
	echo "form submited";
	}
	else{
		echo "email and confirm emails are not same";
	}
}

?>

<h1>Disable Cut, Copy and Paste using jQuery  </h1>

<form method="post" action="" autocomplete="off">

Email: 
<input type="email" id="email" name="email" ><br/>

Confirm Email:  
<input type="email" id="confirmEmail" name="confirmEmail" oncopy="return false" onpaste="return false"><br/>

<input type="submit" value="submit" name="submit" />

</form>


</body>
</html>
 

To disable for both email and confirm email, you can use jQuery script as shown below.

 

<script>

$(document).ready(function(){
    $("#email,#confirmEmail").bind("cut copy paste",function(e) {    // e is event
        e.preventDefault();   // prevents to submit to server(PHP)
        alert("copy,paste is disabled!");   // shows alert
    });
});

</script>  
 
 

 Result:

prevent-copy-paste-by-coding-zon


conclusion

In this tutorial, you learn

how to prevent text selection for confirmation email text box.

Please comment how this article helped you.

 

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP