辛辛苦苦整合的TextArea设置MaxLength的代码
2010-01-19 09:21
239 查看
Javascript代码
view plaincopy to clipboardprint?
function SetTextAreaMaxLength(controlId,length)
{
// JScript File for TextArea
// Keep user from entering more than maxLength characters
function doKeypress(control,length){
maxLength = length;
value = control.value;
if(maxLength && value.length > maxLength-1){
event.returnValue = false;
maxLength = parseInt(maxLength);
}
}
// Cancel default behavior
function doBeforePaste(control,length){
maxLength = length;
if(maxLength)
{
event.returnValue = false;
}
}
// Cancel default behavior and create a new paste routine
function doPaste(control,length){
maxLength = length;
value = control.value;
if(maxLength){
event.returnValue = false;
maxLength = parseInt(maxLength);
var oTR = control.document.selection.createRange();
var iInsertLength = maxLength - value.length + oTR.text.length;
var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
oTR.text = sData;
}
}
function doDragenter(control,length)
{
maxLength = length;
value = control.value;
if(maxLength){
event.returnValue = false;
}
}
function addEvent(elm, evType, fn, useCapture)
{
if (elm.addEventListener)
{
elm.addEventListener(evType, fn, useCapture);
return true;
}
else if (elm.attachEvent)
{
var r = elm.attachEvent('on' + evType, fn);
return r;
}
else {
elm['on' + evType] = fn;
}
}
function AttacheventTextAreaBeforePaste(obj,length)
{
return function()
{
doBeforePaste(obj,length)
}
}
function AttacheventTextAreaPaste(obj,length)
{
return function()
{
doPaste(obj,length)
}
}
function AttacheventTextAreaKeyPress(obj,length)
{
return function()
{
doKeypress(obj,length)
}
}
function AttacheventTextAreaDragEnter(obj,length)
{
return function()
{
doDragenter(obj,length);
}
}
var obj = document.getElementById(controlId);
addEvent(obj,'keypress',AttacheventTextAreaKeyPress(obj,length),null);
addEvent(obj,'beforepaste',AttacheventTextAreaBeforePaste(obj,length),null);
addEvent(obj,'paste',AttacheventTextAreaPaste(obj,length),null);
addEvent(obj,'dragenter',AttacheventTextAreaDragEnter(obj,length),null);
}
该javascript控制了输入,粘贴,拖放操作
view plaincopy to clipboardprint?
function SetTextAreaMaxLength(controlId,length)
{
// JScript File for TextArea
// Keep user from entering more than maxLength characters
function doKeypress(control,length){
maxLength = length;
value = control.value;
if(maxLength && value.length > maxLength-1){
event.returnValue = false;
maxLength = parseInt(maxLength);
}
}
// Cancel default behavior
function doBeforePaste(control,length){
maxLength = length;
if(maxLength)
{
event.returnValue = false;
}
}
// Cancel default behavior and create a new paste routine
function doPaste(control,length){
maxLength = length;
value = control.value;
if(maxLength){
event.returnValue = false;
maxLength = parseInt(maxLength);
var oTR = control.document.selection.createRange();
var iInsertLength = maxLength - value.length + oTR.text.length;
var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
oTR.text = sData;
}
}
function doDragenter(control,length)
{
maxLength = length;
value = control.value;
if(maxLength){
event.returnValue = false;
}
}
function addEvent(elm, evType, fn, useCapture)
{
if (elm.addEventListener)
{
elm.addEventListener(evType, fn, useCapture);
return true;
}
else if (elm.attachEvent)
{
var r = elm.attachEvent('on' + evType, fn);
return r;
}
else {
elm['on' + evType] = fn;
}
}
function AttacheventTextAreaBeforePaste(obj,length)
{
return function()
{
doBeforePaste(obj,length)
}
}
function AttacheventTextAreaPaste(obj,length)
{
return function()
{
doPaste(obj,length)
}
}
function AttacheventTextAreaKeyPress(obj,length)
{
return function()
{
doKeypress(obj,length)
}
}
function AttacheventTextAreaDragEnter(obj,length)
{
return function()
{
doDragenter(obj,length);
}
}
var obj = document.getElementById(controlId);
addEvent(obj,'keypress',AttacheventTextAreaKeyPress(obj,length),null);
addEvent(obj,'beforepaste',AttacheventTextAreaBeforePaste(obj,length),null);
addEvent(obj,'paste',AttacheventTextAreaPaste(obj,length),null);
addEvent(obj,'dragenter',AttacheventTextAreaDragEnter(obj,length),null);
}
view plaincopy to clipboardprint? <asp:TextBox ID="TextBoxAddress" runat="server" Width="200px" TextMode="MultiLine" Height="113px" MaxLength="10"></asp:TextBox> <script language="javascript" type="text/javascript"> SetTextAreaMaxLength('<%=TextBoxAddress.ClientID %>',10); </script> <asp:TextBox ID="TextBoxAddress" runat="server" Width="200px" TextMode="MultiLine" Height="113px" MaxLength="10"></asp:TextBox> <script language="javascript" type="text/javascript"> SetTextAreaMaxLength('<%=TextBoxAddress.ClientID %>',10); </script>
该javascript控制了输入,粘贴,拖放操作
相关文章推荐
- 辛辛苦苦整合的TextArea设置MaxLength的代码
- TextArea设置MaxLength属性最大输入值的js代码
- TextArea设置MaxLength属性最大输入值的js代码
- TextArea设置MaxLength的代码(未测试在不同浏览器下的兼容性)
- [转]TextArea设置MaxLength属性最大输入值的js代码
- JAVASCRIPT应用篇(5)--设置TEXTAREA的MAXLENGTH属性
- 设置TEXTAREA的MAXLENGTH属性 .
- TextView在代码中设置maxLength
- 代码设置Android EditText的相关问题。输入长度maxLength
- SVN 的使用:用两次就会了,很简单.注意总结【①做完自己代码,首先右键项目>team >“与资源库同步”,把队友的代码更新下来(而不是直接提交),②整合完设置"冲突已解决",才能提交】===
- Android 代码设置密码输入框内容的显示/隐藏
- android 用代码设置margin属性,textView加粗
- Yii——使用CHtml::link()等方法时,设置htmlOptions属性可快速生成js代码和ajax请求
- eclipse代码自动提示设置、如何配置eclipse的代码自动提示功能?
- Winsock例子代码:初始化、阻塞/非阻塞、超时设置、select等
- Jquery提供了获得和设置元素的value的方法。如input, textarea等元素的value值。
- android设置全屏壁纸代码
- 底牌项目中设置论坛中各个模块头图的代码