辛辛苦苦整合的TextArea设置MaxLength的代码
2009-07-25 23:30
429 查看
Javascript代码
HTML代码
该javascript控制了输入,粘贴,拖放操作
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); }
HTML代码
<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 >“与资源库同步”,把队友的代码更新下来(而不是直接提交),②整合完设置"冲突已解决",才能提交】===
- Eclipse和FlexBuilder中设置编辑代码高亮
- 【代码】Android: 怎样设置app不被系统k掉
- Android代码jar打包Library项目整合
- 实现会员管理的会员类别设置功能的代码(二十)
- 使用C#代码设置系统日期和时间
- Android 代码设置密码输入框内容的显示/隐藏
- android 用代码设置margin属性,textView加粗
- eclipse代码自动提示设置、如何配置eclipse的代码自动提示功能?