0
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function onPasteMe(pObj) 
{     
// validate  pasted text     
var input  = window.clipboardData.getData('Text'); 
document.getElementById("txt1").value = input ;
var m= stripCharacters(input,pObj) ;
    if(m)
    {
 input=input.replace(/[\s]/g,'\r'); 
 var n=input.split(/\r/g).length; 
 alert(n);
    }
  }

function stripCharacters(input,pObj) {
var r = new RegExp("[^\\s\\r\\t\\n0-9]", "g");
var find = input.match(r) ;      
if(find)
   {   
 alert('String contains both alpha-numeric or your pre-defined special characters!');  
 pObj.innerHTML = ""; 
 return false; 
   }
 return true; 
}
</script>
</head>
<body>
<textarea cols=19 id = "txt1" onPaste="onPasteMe(this);"></textarea>
</body>
</html>

if i paste numbers in the text area like 1233445 the pasted value should present in the text area, but if i pasted values like assdfsdf 123243' or 2313adsdad the text area should get clear automatically???

i used below codes but its not worked for me please help,
pObj.innerHTML = "";
document.getElementById("txt1").value =""; 
jcrshankar
  • 807
  • 6
  • 18
  • 34

2 Answers2

1
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function onPasteMe(pObj) 
{     
// validate  pasted text     
var input  = window.clipboardData.getData('Text'); 
document.getElementById("txt1").value = input ;
var m= stripCharacters(input,pObj) ;
    if(m)
    {
 input=input.replace(/[\s]/g,'\r'); 
 var n=input.split(/\r/g).length; 
 alert(n);
    }
    return false;
  }

function stripCharacters(input,pObj) {
var r = new RegExp("[^\\s\\r\\t\\n0-9]", "g");
var find = input.match(r) ;      
if(find)
   {   
 alert('String contains both alpha-numeric or your pre-defined special characters!');  
 pObj.value = ""; 
 return false; 
   }
 return true; 
}
</script>
</head>
<body>
<textarea cols=19 id = "txt1" onPaste="return onPasteMe(this);"></textarea>
</body>
</html>
  1. you should use pObj.value="" to empty the textarea.
  2. you need return false for onPaste() function.
GordonZ
  • 345
  • 1
  • 2
  • 9
-1

First, onPaste isn't fully supported
Reference: JavaScript get clipboard data on paste event (Cross browser)

Community
  • 1
  • 1
Jared Drake
  • 972
  • 4
  • 12