web文本框限制字数经典代码

 

 

2、第二种

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <mce:script language="javascript"><!-- String.prototype.len=function(){ return this.replace(/[^/x00-/xff]/g,"**").length; } //Set maxlength for multiline TextBox function setMaxLength(object,length) { var result = true; var controlid = document.selection.createRange().parentElement().id; var controlValue = document.selection.createRange().text; var tempString=object.value; var tt=""; for(var i=0;i<length;i++) { if(tt.len()<length) tt=tempString.substr(0,i+1); else break; } if(tt.len()>length) tt=tt.substr(0,tt.length-1); object.value=tt; } //Check maxlength for multiline TextBox when paste function limitPaste(object,length) { var tempLength = 0; if(document.selection) { if(document.selection.createRange().parentElement().id == object.id) { tempLength = document.selection.createRange().text.len(); } } var tempValue = window.clipboardData.getData("Text"); tempLength = object.value.len() + tempValue.len() - tempLength; if (tempLength > length) { tempLength -= length; var tt=""; for(var i=0;i<tempValue.len()-tempLength;i++) { if(tt.len()<(tempValue.len()-tempLength)) tt=tempValue.substr(0,i+1); else break; } if(tt.len()<=0) { window.event.returnValue=false; } else { tempValue=tt; window.clipboardData.setData("Text", tempValue); window.event.returnValue = true; } } } function PressLength() { if(event.srcElement.type=="text" || event.srcElement.type=="textarea" ) { if(event.srcElement.length!=null) setMaxLength(event.srcElement,event.srcElement.length); } } function LimitLength() { if(event.srcElement.type=="text" || event.srcElement.type=="textarea" ) { if(event.srcElement.length!=null) limitPaste(event.srcElement,event.srcElement.length); } } document.documentElement.attachEvent('onkeyup', PressLength); document.documentElement.attachEvent('onpaste', LimitLength); // --></mce:script> </HEAD> <body MS_POSITIONING="GridLayout"> <form name="Form1" method="post" action="WebForm1.aspx" id="Form1">   <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="600" border="0"> <TR> <TD><FONT face="宋体">控制文本字数方案</FONT></TD> <TD><FONT face="宋体"></FONT></TD> <TD><FONT face="宋体"></FONT></TD> </TR> <TR> <TD><INPUT type="text" length="5" id="Text1" name="Text1"></TD> <TD><FONT face="宋体">控制字符为5个</FONT></TD> <TD></TD> </TR> <TR> <TD> <TEXTAREA rows="14" length="10" cols="39" id="Textarea1" name="Textarea1" style="WIDTH: 336px; HEIGHT: 232px">

 

 

 

 

 

 

 

你可能感兴趣的:(JavaScript,Web,function,Microsoft,webform,textbox)