0

Can anybody tell me how to find if clipboard data contains enter (next line) and tab or not

I have code for paste event as:

document.addEventListener('paste',function(event){
    if(event.clipboardData.types.indexOf('text/plain') > -1){
        var data=event.clipboardData.getData('text/plain');
        event.preventDefault();
    }
 });

I want to print a alert if data contains tab or enter (nextline).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Madan
  • 380
  • 1
  • 4
  • 11

1 Answers1

1

var data = "merry christmas\n"

if(/\n|\t/.test(data)) alert("contains tab or new line")

wich would be like that in your code:

document.addEventListener('paste',function(event){
    if(event.clipboardData.types.indexOf('text/plain') > -1){
        var data=event.clipboardData.getData('text/plain');
        if(/\n|\t/.test(data)) {
            // contains tab or new line
        }
        event.preventDefault();
    }
 });
CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • does clipboard data also has \n or \t? – Madan Dec 25 '15 at 22:41
  • @Madan I added an example for your code, is that ok? – CoderPi Dec 25 '15 at 22:42
  • actually i was building a simple editor using js where i need to handle copy and paste. and i did this for copy:`document.addEventListener('copy',function(event){ var copiedData=dataToCopy(); event.clipboardData.setData('text/plain',copiedData); event.preventDefault(); });` – Madan Dec 25 '15 at 23:25
  • thanks @CodeSir it worked for me now. I made some changes on my code. – Madan Dec 26 '15 at 00:20