4
Answers

Copy Paste Issue

My textBox some times does not allow me to paste, it works for 5 or 6th attempt, sometimes in the very first attempt. I happens to me both in Crome and IE, below is the code

function AlphaNumericPaste() {
var objTxtBox = window.event.srcElement;
var num_regex = /^[\w]+$/;
var PasteData = TrimclipboardData();
if (!num_regex.test(PasteData)) {
CancelEvent();
}
}

Answers (4)
1
Puneet Kankar

Puneet Kankar

241 8k 1.1m 6y
Please check given below link maybe helpful for you.
  1. $('#email').bind("cut copy paste",function(e) {  
  2.      e.preventDefault();  
  3.  });  
https://stackoverflow.com/questions/5510129/how-to-disable-paste-ctrlv-with-jquery 
0
Muthu Kumar

Muthu Kumar

NA 3k 278.1k 6y
Here is an example for you,

  1. ['cut''copy''paste'].forEach(function(event) {  
  2.     document.addEventListener(event, function(e) {  
  3.         console.log(event);     
  4.     });  
  5. });  
  6.               
You can may use some advance concept for copy and paste thing,

Reference:-

https://www.lucidchart.com/techblog/2014/12/02/definitive-guide-copying-pasting-javascript/

https://css-tricks.com/copy-paste-the-web/

https://stackoverflow.com/questions/686995/catch-paste-input
0
Mansoor Mohammed

Mansoor Mohammed

NA 126 0 6y
Hi Neigel
Here are all the functions from my  js file
 
 
function NumericOnly() {
if (event.keyCode < 48 || event.keyCode > 57) {
CancelEvent();
}
}
function NumericPaste() {
var objTxtBox = window.event.srcElement;
var num_regex = /^\d+$/;
var PasteData = TrimclipboardData();
if (!num_regex.test(PasteData)) {
CancelEvent();
}
}
function AlphaNumericOnly() {
if (!((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 97 && event.keyCode <= 122))) {
CancelEvent();
}
}
function AlphaNumericPaste() {
var objTxtBox = window.event.srcElement;
var num_regex = /^[\w]+$/;
var PasteData = TrimclipboardData();
if (!num_regex.test(PasteData)) {
CancelEvent();
}
}
/*function AlphaNumericOnlyDash() {
AlphaNumericDashPaste(String.fromCharCode(event.keyCode));
}
function AlphaNumericDashPaste(strTest) {
var num_regex = /^[a-zA-Z0-9 -]*$/;
CheckRegEx(num_regex, strTest);
}
*/
function AlphaNumericSpaceOnly() {
if (!((event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode == 32 || (event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 97 && event.keyCode <= 122))) {
CancelEvent();
}
}
//event.keyCode == 44 is added to resolve JIRA 1702
function AlphaNumericOnlyDash() {
if (!((event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode == 44 || event.keyCode == 45 || (event.keyCode >= 65 && event.keyCode <= 90) || (event.keyCode >= 97 && event.keyCode <= 122))) {
CancelEvent();
}
}
function AlphaNumericDashPaste() {
var objTxtBox = window.event.srcElement;
var num_regex = /^[\w-]+$/;
var PasteData = TrimclipboardData();
if (!num_regex.test(PasteData)) {
CancelEvent();
}
}
function AlphaNumericSpacePaste() {
var objTxtBox = window.event.srcElement;
var num_regex = /^[a-zA-Z0-9 ]*$/;
var PasteData = TrimclipboardData();
if (!num_regex.test(PasteData)) {
CancelEvent();
}
}
function NumericOnlyWildcard() {
var strValue = event.srcElement.value;
var strNoStar = strValue.replace(/\*/g, "");
if (event.keyCode == 42 && (strValue.length - strNoStar.length > 1)) {
CancelEvent();
}
if (!(event.keyCode > 47 && event.keyCode < 58 || event.keyCode == 42)) {
CancelEvent();
}
}
function NumericWildcardPaste() {
var objTxtBox = window.event.srcElement;
var num_regex = /^[\d*]+$/;
var PasteData = TrimclipboardData();
if (!num_regex.test(PasteData)) {
CancelEvent();
}
}
function NumericOnlyDash() {
if (!(event.keyCode > 47 && event.keyCode < 58 || event.keyCode == 45)) {
CancelEvent();
}
}
function NumericDashPaste() {
var objTxtBox = window.event.srcElement;
var num_regex = /^[\d-]+$/;
var PasteData = TrimclipboardData();
if (!num_regex.test(PasteData)) {
CancelEvent();
}
}
function ValidKeyOnly() {
ValidKeyPaste(String.fromCharCode(event.keyCode));
}
function ValidKeyPaste(strTest) {
var num_regex = /^[\w-+,. \/:?\'"()$@[\]\n\r\t]+$/;
CheckRegEx(num_regex, strTest);
}
function CurrencyOnly() {
CurrencyPaste(String.fromCharCode(event.keyCode));
}
function CurrencyPaste(strTest) {
var num_regex = /^\-?\$?\x20?(\d{1,3}(\,\d{3})*|(\d+))?(\.?\d?\d?)?$/;
CheckRegEx(num_regex, strTest);
}
function PositiveAmountOnly() {
PositiveAmountPaste(String.fromCharCode(event.keyCode));
}
function PositiveAmountPaste(strTest) {
var num_regex = /^\$?\x20?(\d{1,3}(\,\d{3})*|(\d+))?(\.?\d?\d?)?$/;
CheckRegEx(num_regex, strTest);
}
function CityOnly() {
CityPaste(String.fromCharCode(event.keyCode));
}
function CityPaste(strTest) {
var city_regex = /^[a-zA-Z\x20\x2e]+?$/;
CheckRegEx(city_regex, strTest);
}
function CheckRegEx(regex, strTest) {
var strValue = event.srcElement.value;
var strSelected = getSelectionText();
var TestData = strTest;
if (typeof (strTest) == "undefined") {
TestData = TrimclipboardData();
}
if (strSelected.length > 0) {
strValue = strValue.replace(strSelected, TestData);
} else {
strValue += TestData;
}
if (!regex.test(strValue)) {
CancelEvent();
}
}
function WordOnly() {
WordPaste(String.fromCharCode(event.keyCode));
}
function WordPaste(strTest) {
var objTxtBox = window.event.srcElement;
var num_regex = /^[\w\x20\x2e]+$/;
var TestData = strTest;
if (typeof (strTest) == "undefined") {
TestData = TrimclipboardData();
}
if (!num_regex.test(TestData)) {
CancelEvent();
}
}
function getSelectionText() {
var text = "";
if (typeof (document.selection) != "undefined") {
text = document.selection.createRange().text;
} else if (window.event.srcElement.selectionStart) {
text = window.event.srcElement.value.substring(window.event.srcElement.selectionStart, window.event.srcElement.selectionEnd);
}
return text;
}
function Trim(strInput) {
return strInput.replace(/^\s+|\s+$/g, '');
}
function TrimclipboardData() {
var strOutput = Trim(window.clipboardData.getData("Text"));
window.clipboardData.setData("Text", strOutput);
return strOutput;
}
function CancelEvent() {
event.returnValue = false;
if (!document.all) {
event.preventDefault();
}
}
0
Nigel Fernandes

Nigel Fernandes

381 4.2k 1.5m 6y
hi can you share more information so we can help you?
 
What technologies you are using ? 
Share the html page