<!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 ="";
答案 0 :(得分: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 :(得分:-1)
首先,onPaste不完全支持
参考:JavaScript get clipboard data on paste event (Cross browser)