简体   繁体   中英

how to clear the textarea in onpaste method?

<!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 =""; 
<!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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM