复制粘贴 [英] copy paste
本文介绍了复制粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有多行的文本框.我使用Java脚本限制了最大长度.我的问题是,当我使用键盘输入值时,它会验证我的最大长度,但是当我复制文本并将其粘贴时,同样会接受比给定最大长度更多的值.复制内容并将其粘贴到文本框中时,如何限制最大长度?
I have a textbox with multilines. I am restricting the max length using java script. My problem is, when I enter the values using keyboard it''s validating my max length but when I copy a text and paste it the same is accepting the values more than the given max length. How can I restrict the max length when I copy and paste the content to the textbox?
推荐答案
您可以检查在键盘上的粘贴.如果键码= 86,请检查长度.
You can check the paste on the keyup. If the keycode = 86, check the length.
<HTML><head></head><body>
<form name="myForm">
Text: <input type="text" name="myText"
onKeyUp = "fncKeyLength(this);">
</form>
<script>
function fncKeyLength(text){
if (window.event.ctrlKey){
if (window.event.keyCode == 86) {
alert("<br />The string is this long: " + text.length);
}
}}
</script></body></HTML>
好,请检查此代码.
Ok, Check with this code.
<html>
<head>
</head>
<body>
<form id="myForm">
<asp:textbox runat="server" id="TextBox1" maxlength="40" onpaste="fncCheckLength()">
function fncCheckLength()
{
var textBox = document.myForm.TextBox1;
if(textBox.value.length > MaxLength)
{
alert("Text too long");
textBox.value = "";
}
}
</body>
</html>
对于键盘检查,正确的代码是(但对于右键单击,Ankur备注为true):
For the keyboard check, the correct code is (but the Ankur remark is true for right click):
<html>
<head>
</head>
<body>
<form>
Text: <input type="text" name="myText"
onKeyUp = "fncKeyLength();">
</form>
<script>
function fncKeyLength()
{
var key;
var IsCtrl = false;
if (window.event)
{
key = window.event.keyCode; //IE
IsCtrl = window.event.ctrlKey;
}
else
{
key = e.which; //firefox
IsCtrl = e.ctrlKey;
}
if( IsCtrl && (key == 118 || key == 86 ) )
{
fncCheckLength();
}
}
function fncCheckLength()
{
var textBox = document.myForm.TextBox1;
if(textBox.value.length > MaxLength)
{
alert("Text too long");
textBox.value = "";
}
}
</body>
</html>
在TextBox中添加 onpaste ="return false;" ,
Add onpaste="return false;" in TextBox,
<asp:textbox runat="server" id="txtRef" maxlength="40" onpaste="return false;">
这篇关于复制粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文