Disable Cut,Copy and Paste using jQuery
How to prevent cut, copy, paste in text box.
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.
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.
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:
conclusion
In this tutorial, you learn
how to prevent text selection for confirmation email text box.
Please comment how this article helped you.
Comments
Post a Comment