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>
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>