js代码小记(判断textbox的长度,并且固定最多收入字符)

本文介绍了一个简单的字符计数器的实现方法,该计数器能够实时显示输入文本区域中剩余可输入的字符数量,当超出限制时会给出提示。
None.gif <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml" >
None.gif
< head >
None.gif
< meta  http-equiv ="Content-Type"  content ="text/html; charset=gb2312"   />
None.gif
< title > Untitled Document </ title >
ExpandedBlockStart.gifContractedBlock.gif
< SCRIPT  language =javascript1.3 > dot.gif <!--
InBlock.gif
var testleng=300;//允许多少字符串填入
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif
function GC_UpdateCharCount() dot.gif{
InBlock.gif  
var desc_el = document.getElementById('desc');
InBlock.gif  
var remainingchars_el = document.getElementById('remainingchars');
InBlock.gif
InBlock.gif  
var num_remaining = testleng - desc_el.value.length;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if (num_remaining >= 0dot.gif{
InBlock.gif  remainingchars_el.innerHTML 
= num_remaining;
ExpandedSubBlockStart.gifContractedSubBlock.gif  }
 else dot.gif{
InBlock.gif  remainingchars_el.innerHTML 
= "<font color=red>" +
InBlock.gif  (
-num_remaining + '') + " characters over</font>";
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif}

InBlock.gif
function isOver(sText)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
var intlen=sText.value.length;
InBlock.gif                
if (intlen>testleng)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    alert(
"描述特征的内容的字数必修小于或者等于 "+testleng);
InBlock.gif                    sText.focus();
InBlock.gif                    sText.select();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
//--></SCRIPT>
InBlock.gif
</head>
InBlock.gif
InBlock.gif
<body>
InBlock.gif
<<B>Group description</B><BR><TEXTAREA id=desc onkeydown=GC_UpdateCharCount() onkeyup=GC_UpdateCharCount()  onblur="isOver(this);"style="WIDTH: 99%" name=desc rows=4 cols=50></TEXTAREA> 
InBlock.gif            
<BR>Letters remaining: <SPAN id=remainingchars>
InBlock.gif            
<SCRIPT language=javascript1.2><!--
InBlock.gif              GC_UpdateCharCount();
InBlock.gif              
//--></SCRIPT></SPAN><NOSCRIPT>300 characters allowed </NOSCRIPT>
InBlock.gif
</body>
InBlock.gif
</html>
InBlock.gif
效果参考: www.lostfound.cn/CoutChar.html

转载于:https://www.cnblogs.com/xucanzhao/archive/2006/06/08/420594.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值