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>

如果我在文本区域中粘贴数字,如 1233445,则粘贴的值应显示在文本区域中,但如果我粘贴诸如 assdfsdf 123243' 或 2313adsdad 之类的值,则文本区域应自动清除???

i used below codes but its not worked for me please help,
pObj.innerHTML = "";
document.getElementById("txt1").value =""; 
4

2 回答 2

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. 您应该使用 pObj.value="" 清空文本区域。
  2. 您需要为 onPaste() 函数返回 false。
于 2012-08-20T05:53:31.840 回答
-1

首先,不完全支持 onPaste
参考:JavaScript get clipboard data on paste event (Cross browser)

于 2012-08-20T05:15:31.363 回答