jQuery noPaste plugin
This jQuery plugin prevents the visitors from using paste methods in any form of inputs.
Usage
-
<input type="text" name="confirm_email" class="noPaste" />
-
-
<script language="text/javascript">
-
$(document).ready(function(){
-
$("input.noPaste").noPaste();
-
});
-
</script>
Compiled code
-
/**
-
* jQuery noPaste plugin
-
* This function prevents the user from using copy/paste functions in inputs.
-
*
-
* @author Daniel Lindén <admin@vebut.se>
-
* @version 1.0
-
*/
-
jQuery.fn.noPaste=function(){var a=$(this).val();$(this).keyup(function(){var b=$(this).val();b.length>a.length&&b.length!=a.length+1&&$(this).val(a);a=b});$(this).change(function(){$(this).val()!=a&&$(this).val(a)})};
Source code
-
/**
-
* jQuery noPaste plugin
-
* This function prevents the user from using copy/paste functions in inputs.
-
*
-
* @author Daniel Lindén <admin@vebut.se>
-
* @version 1.0
-
*/
-
jQuery.fn.noPaste = function ()
-
{
-
// Original field value, later on this will hold the value of the field.
-
var inputStr = $(this).val();
-
-
// Add keyup event handler
-
$(this).keyup(function(){
-
// New field value after keyup event
-
var inputChange = $(this).val();
-
-
// Count the length of the new input value and check its length.
-
// The length diff must not be greater than 1.
-
if (inputChange.length > inputStr.length && inputChange.length != (inputStr.length + 1)) {
-
$(this).val(inputStr); // Reset the field value to its earlier value
-
}
-
-
// Update original value to match the new value if it validates.
-
inputStr = inputChange;
-
});
-
-
// Block mouse paste
-
$(this).change(function(){
-
// If the value is not equal to the earlier value
-
if ($(this).val() != inputStr) {
-
$(this).val(inputStr); // Reset the value
-
}
-
});
-
};
Leave a Reply