多行文本框限制输入字符长度(两种方法)

方法一:弹出对话框提示
1.html代码

None.gif < HTML >
None.gif    
< HEAD >
None.gif        
< title > WebForm6 </ title >
None.gif        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
None.gif        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
None.gif        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
ExpandedBlockStart.gifContractedBlock.gif        
< script  language ="javascript" > dot.gif             
InBlock.gif            
function isOver(sText,len)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
var intlen=sText.value.length;
InBlock.gif                
if (intlen>len)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    alert(
"The content length must Less than or Equal "+len);
InBlock.gif                    sText.focus();
InBlock.gif                    sText.select();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        
</ script >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< asp:TextBox  id ="txtName"  style ="Z-INDEX: 102; LEFT: 200px; POSITION: absolute; TOP: 104px"  runat ="server"
None.gif                TextMode
="MultiLine"  Height ="112px"  Width ="271px" ></ asp:TextBox >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
2.cs代码
None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
this.txtName.Attributes.Add("onblur","isOver(this,1000);");
ExpandedBlockEnd.gif        }
方法二:限制用户继续输入
None.gif < HEAD >
None.gif    
<!--  TWO STEPS TO INSTALL LIMIT TEXTAREA:
None.gif
None.gif  1.  Copy the coding into the HEAD of your HTML document
None.gif  2.  Add the last code into the BODY of your HTML document  
-->
None.gif    
<!--  STEP ONE: Paste this code into the HEAD of your HTML document   -->
ExpandedBlockStart.gifContractedBlock.gif    
< SCRIPT  LANGUAGE ="JavaScript" > dot.gif
InBlock.gif
<!-- Original:  Ronnie T. Moore -->
InBlock.gif
<!-- Web Site:  The JavaScript Source -->
InBlock.gif
InBlock.gif
<!-- Dynamic 'fix' by: Nannette Thacker -->
InBlock.gif
<!-- Web Site: http://www.shiningstar.net -->
InBlock.gif

InBlock.gif
<!-- This script and many more are available free online at -->
InBlock.gif
<!-- The JavaScript Source!! http://javascript.internet.com -->
InBlock.gif

InBlock.gif
<!-- Begin
ExpandedSubBlockStart.gifContractedSubBlock.gif
function textCounter(field, countfield, maxlimit) dot.gif{
InBlock.gif
if (field.value.length > maxlimit) // if too longdot.giftrim it!
InBlock.gif
field.value = field.value.substring(0, maxlimit);
InBlock.gif
// otherwise, update 'characters left' counter
InBlock.gif
else 
InBlock.gifcountfield.value 
= maxlimit - field.value.length;
ExpandedSubBlockEnd.gif}

InBlock.gif
// End -->
ExpandedBlockEnd.gif
    
</ SCRIPT >
None.gif    
<!--  STEP TWO: Copy this code into the BODY of your HTML document   -->
None.gif
</ HEAD >
None.gif
< BODY >
None.gif    
<!--  textCounter() parameters are:  text field, the count field, max length  -->
None.gif    
< center >
None.gif        
< form  name ="myform"  action ="YOUR-SCRIPT.CGI" >
None.gif            
< font  size ="1"  face ="arial, helvetica, sans-serif" > ( You may enter up to 125 
None.gif                characters. )
< br >
None.gif                
< textarea  name ="message"  wrap ="physical"  cols ="28"  rows ="4"  onKeyDown ="textCounter(this.form.message,this.form.remLen,125);"
None.gif                    onKeyUp
="textCounter(this.form.message,this.form.remLen,125);" ></ textarea >
None.gif                
< br >
None.gif                
< input  readonly type ="text"  name ="remLen"  size ="3"  maxlength ="3"  value ="125" >  characters 
None.gif                left
</ font >
None.gif        
</ form >
None.gif    
</ center >
None.gif    
< p >< center >
None.gif            
< font  face ="arial, helvetica"  SIZE ="-2" > Free JavaScripts provided < br >
None.gif                by 
< href ="http://javascriptsource.com" > The JavaScript Source </ a ></ font >
None.gif        
</ center >
None.gif
</ BODY >

转载于:https://www.cnblogs.com/wking108/archive/2006/09/14/504031.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值